Skip to content

Commit e27d66e

Browse files
committed
.
1 parent 9842811 commit e27d66e

File tree

2,481 files changed

+176210
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,481 files changed

+176210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Title: Basic Node Example
3+
* Description: Simple file that declares a few functions and invokes them.
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// Dependencies
11+
var mathLib = require('./lib/math');
12+
var jokesLib = require('./lib/jokes');
13+
14+
15+
// App object
16+
var app = {};
17+
18+
19+
// Configuration
20+
app.config = {
21+
'timeBetweenJokes' : 1000
22+
};
23+
24+
25+
// Function that prints a random joke
26+
app.printAJoke = function(){
27+
28+
// Get all the jokes
29+
var allJokes = jokesLib.allJokes();
30+
31+
// Get the length of the jokes
32+
var numberOfJokes = allJokes.length;
33+
34+
// Pick a random number between 1 and the number of jokes
35+
var randomNumber = mathLib.getRandomNumber(1,numberOfJokes);
36+
37+
// Get the joke at that position in the array (minus one)
38+
var selectedJoke = allJokes[randomNumber - 1];
39+
40+
// Send the joke to the console
41+
console.log(selectedJoke);
42+
};
43+
44+
45+
// Function that loops indefinitely, calling the printAJoke function as it goes
46+
app.indefiniteLoop = function(){
47+
48+
// Create the interval, using the config variable defined above
49+
setInterval(app.printAJoke,app.config.timeBetweenJokes);
50+
};
51+
52+
53+
// Invoke the loop
54+
app.indefiniteLoop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Title: Jokes Library
3+
* Description: Utility library for getting a list of Jokes
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// Dependencies
11+
var fs = require('fs');
12+
13+
// App object
14+
var jokes = {};
15+
16+
// Get all the jokes and return them to the user
17+
jokes.allJokes = function(){
18+
19+
// Read the text file containing the jokes
20+
var fileContents = fs.readFileSync(__dirname+'/jokes.txt', 'utf8');
21+
22+
// Turn the string into an array
23+
var arrayOfJokes = fileContents.split(/\r?\n/);
24+
25+
// Return the array
26+
return arrayOfJokes;
27+
28+
};
29+
30+
// Export the library
31+
module.exports = jokes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Why should you avoid the duck psychologist? He's a quack.
2+
What do cows do in their spare time? Go to the moooovies.
3+
Why do birds hate Instagram? They prefer to tweet.
4+
Where do fish keep all their money? The river bank.
5+
Why did the sheep get detention? It was baaaaaad.
6+
Why couldn't the horse congress pass any bills? They all kept voting naaaaaay.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Title: Math Library
3+
* Description: Utility library for math-related functions
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// App object
11+
var math = {};
12+
13+
// Get a random integer between two integers
14+
// Inspired by: http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript
15+
math.getRandomNumber = function(min,max){
16+
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0;
17+
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0;
18+
return Math.floor(Math.random()*(max-min+1)+min);
19+
};
20+
21+
22+
// Export the library
23+
module.exports = math;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//registry.npmjs.org/:_authToken=xxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxxxxx
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Title: Basic Node Example
3+
* Description: Simple file that declares a few functions and invokes them.
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// Dependencies
11+
var mathLib = require('./lib/math');
12+
var jokesLib = require('./lib/jokes');
13+
14+
15+
// App object
16+
var app = {};
17+
18+
19+
// Configuration
20+
app.config = {
21+
'timeBetweenJokes' : 1000
22+
};
23+
24+
25+
// Function that prints a random joke
26+
app.printAJoke = function(){
27+
28+
// Get all the jokes
29+
var allJokes = jokesLib.allJokes();
30+
31+
// Get the length of the jokes
32+
var numberOfJokes = allJokes.length;
33+
34+
// Pick a random number between 1 and the number of jokes
35+
var randomNumber = mathLib.getRandomNumber(1,numberOfJokes);
36+
37+
// Get the joke at that position in the array (minus one)
38+
var selectedJoke = allJokes[randomNumber - 1];
39+
40+
// Send the joke to the console
41+
console.log(selectedJoke);
42+
};
43+
44+
45+
// Function that loops indefinitely, calling the printAJoke function as it goes
46+
app.indefiniteLoop = function(){
47+
48+
// Create the interval, using the config variable defined above
49+
setInterval(app.printAJoke,app.config.timeBetweenJokes);
50+
};
51+
52+
53+
// Invoke the loop
54+
app.indefiniteLoop();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Title: Jokes Library
3+
* Description: Utility library for getting a list of Jokes
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// Dependencies
11+
var fs = require('fs');
12+
13+
// App object
14+
var jokes = {};
15+
16+
// Get all the jokes and return them to the user
17+
jokes.allJokes = function(){
18+
19+
// Read the text file containing the jokes
20+
var fileContents = fs.readFileSync(__dirname+'/jokes.txt', 'utf8');
21+
22+
// Turn the string into an array
23+
var arrayOfJokes = fileContents.split(/\r?\n/);
24+
25+
// Return the array
26+
return arrayOfJokes;
27+
28+
};
29+
30+
// Export the library
31+
module.exports = jokes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Why should you avoid the duck psychologist? He's a quack.
2+
What do cows do in their spare time? Go to the moooovies.
3+
Why do birds hate Instagram? They prefer to tweet.
4+
Where do fish keep all their money? The river bank.
5+
Why did the sheep get detention? It was baaaaaad.
6+
Why couldn't the horse congress pass any bills? They all kept voting naaaaaay.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Title: Math Library
3+
* Description: Utility library for math-related functions
4+
* Author: Leslie Lewis
5+
* Date: 10/24/17
6+
*
7+
*/
8+
9+
10+
// App object
11+
var math = {};
12+
13+
// Get a random integer between two integers
14+
// Inspired by: http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript
15+
math.getRandomNumber = function(min,max){
16+
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0;
17+
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0;
18+
return Math.floor(Math.random()*(max-min+1)+min);
19+
};
20+
21+
22+
// Export the library
23+
module.exports = math;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "basicExample",
3+
"version": "0.0.1",
4+
"license": "UNLICENSED",
5+
"private": "true",
6+
"description": "Simple file that declares a few functions and invokes them",
7+
"main": "index.js",
8+
"scripts": {
9+
"start": "node index.js"
10+
},
11+
"dependencies": {
12+
"jokes" : "0.1.3"
13+
}
14+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Create and export configuration variables
3+
*
4+
*/
5+
6+
// Container for all environments
7+
var environments = {};
8+
9+
// Staging (default) environment
10+
environments.staging = {
11+
'port' : 3000,
12+
'envName' : 'staging'
13+
};
14+
15+
// Production environment
16+
environments.production = {
17+
'port' : 5000,
18+
'envName' : 'production'
19+
};
20+
21+
// Determine which environment was passed as a command-line argument
22+
var currentEnvironment = typeof(process.env.NODE_ENV) == 'string' ? process.env.NODE_ENV.toLowerCase() : '';
23+
24+
// Check that the current environment is one of the environments above, if not default to staging
25+
var environmentToExport = typeof(environments[currentEnvironment]) == 'object' ? environments[currentEnvironment] : environments.staging;
26+
27+
// Export the module
28+
module.exports = environmentToExport;
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Primary file for API
3+
*
4+
*/
5+
6+
// Dependencies
7+
var http = require('http');
8+
var url = require('url');
9+
var StringDecoder = require('string_decoder').StringDecoder;
10+
var config = require('./config');
11+
12+
// Configure the server to respond to all requests with a string
13+
var server = http.createServer(function(req,res){
14+
15+
// Parse the url
16+
var parsedUrl = url.parse(req.url, true);
17+
18+
// Get the path
19+
var path = parsedUrl.pathname;
20+
var trimmedPath = path.replace(/^\/+|\/+$/g, '');
21+
22+
// Get the query string as an object
23+
var queryStringObject = parsedUrl.query;
24+
25+
// Get the HTTP method
26+
var method = req.method.toLowerCase();
27+
28+
//Get the headers as an object
29+
var headers = req.headers;
30+
31+
// Get the payload,if any
32+
var decoder = new StringDecoder('utf-8');
33+
var buffer = '';
34+
req.on('data', function(data) {
35+
buffer += decoder.write(data);
36+
});
37+
req.on('end', function() {
38+
buffer += decoder.end();
39+
40+
// Check the router for a matching path for a handler. If one is not found, use the notFound handler instead.
41+
var chosenHandler = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound;
42+
43+
// Construct the data object to send to the handler
44+
var data = {
45+
'trimmedPath' : trimmedPath,
46+
'queryStringObject' : queryStringObject,
47+
'method' : method,
48+
'headers' : headers,
49+
'payload' : buffer
50+
};
51+
52+
// Route the request to the handler specified in the router
53+
chosenHandler(data,function(statusCode,payload){
54+
55+
// Use the status code returned from the handler, or set the default status code to 200
56+
statusCode = typeof(statusCode) == 'number' ? statusCode : 200;
57+
58+
// Use the payload returned from the handler, or set the default payload to an empty object
59+
payload = typeof(payload) == 'object'? payload : {};
60+
61+
// Convert the payload to a string
62+
var payloadString = JSON.stringify(payload);
63+
64+
// Return the response
65+
res.setHeader('Content-Type', 'application/json');
66+
res.writeHead(statusCode);
67+
res.end(payloadString);
68+
console.log("Returning this response: ",statusCode,payloadString);
69+
70+
});
71+
72+
});
73+
});
74+
75+
// Start the server
76+
server.listen(config.port,function(){
77+
console.log('The server is up and running on port '+config.port+' in '+config.envName+' mode.');
78+
});
79+
80+
// Define all the handlers
81+
var handlers = {};
82+
83+
// Sample handler
84+
handlers.sample = function(data,callback){
85+
callback(406,{'name':'sample handler'});
86+
};
87+
88+
// Not found handler
89+
handlers.notFound = function(data,callback){
90+
callback(404);
91+
};
92+
93+
// Define the request router
94+
var router = {
95+
'sample' : handlers.sample
96+
};

0 commit comments

Comments
 (0)