forked from pirple/The-NodeJS-Master-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9842811
commit e27d66e
Showing
2,481 changed files
with
176,210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Title: Basic Node Example | ||
* Description: Simple file that declares a few functions and invokes them. | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// Dependencies | ||
var mathLib = require('./lib/math'); | ||
var jokesLib = require('./lib/jokes'); | ||
|
||
|
||
// App object | ||
var app = {}; | ||
|
||
|
||
// Configuration | ||
app.config = { | ||
'timeBetweenJokes' : 1000 | ||
}; | ||
|
||
|
||
// Function that prints a random joke | ||
app.printAJoke = function(){ | ||
|
||
// Get all the jokes | ||
var allJokes = jokesLib.allJokes(); | ||
|
||
// Get the length of the jokes | ||
var numberOfJokes = allJokes.length; | ||
|
||
// Pick a random number between 1 and the number of jokes | ||
var randomNumber = mathLib.getRandomNumber(1,numberOfJokes); | ||
|
||
// Get the joke at that position in the array (minus one) | ||
var selectedJoke = allJokes[randomNumber - 1]; | ||
|
||
// Send the joke to the console | ||
console.log(selectedJoke); | ||
}; | ||
|
||
|
||
// Function that loops indefinitely, calling the printAJoke function as it goes | ||
app.indefiniteLoop = function(){ | ||
|
||
// Create the interval, using the config variable defined above | ||
setInterval(app.printAJoke,app.config.timeBetweenJokes); | ||
}; | ||
|
||
|
||
// Invoke the loop | ||
app.indefiniteLoop(); |
31 changes: 31 additions & 0 deletions
31
Section 2/Anatomy of a Node Application/lib/jokes/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Title: Jokes Library | ||
* Description: Utility library for getting a list of Jokes | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// Dependencies | ||
var fs = require('fs'); | ||
|
||
// App object | ||
var jokes = {}; | ||
|
||
// Get all the jokes and return them to the user | ||
jokes.allJokes = function(){ | ||
|
||
// Read the text file containing the jokes | ||
var fileContents = fs.readFileSync(__dirname+'/jokes.txt', 'utf8'); | ||
|
||
// Turn the string into an array | ||
var arrayOfJokes = fileContents.split(/\r?\n/); | ||
|
||
// Return the array | ||
return arrayOfJokes; | ||
|
||
}; | ||
|
||
// Export the library | ||
module.exports = jokes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Why should you avoid the duck psychologist? He's a quack. | ||
What do cows do in their spare time? Go to the moooovies. | ||
Why do birds hate Instagram? They prefer to tweet. | ||
Where do fish keep all their money? The river bank. | ||
Why did the sheep get detention? It was baaaaaad. | ||
Why couldn't the horse congress pass any bills? They all kept voting naaaaaay. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Title: Math Library | ||
* Description: Utility library for math-related functions | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// App object | ||
var math = {}; | ||
|
||
// Get a random integer between two integers | ||
// Inspired by: http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript | ||
math.getRandomNumber = function(min,max){ | ||
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0; | ||
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0; | ||
return Math.floor(Math.random()*(max-min+1)+min); | ||
}; | ||
|
||
|
||
// Export the library | ||
module.exports = math; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//registry.npmjs.org/:_authToken=xxxxx-xxxxx-xxxxx-xxxx-xxxxxxxxxxxxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Title: Basic Node Example | ||
* Description: Simple file that declares a few functions and invokes them. | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// Dependencies | ||
var mathLib = require('./lib/math'); | ||
var jokesLib = require('./lib/jokes'); | ||
|
||
|
||
// App object | ||
var app = {}; | ||
|
||
|
||
// Configuration | ||
app.config = { | ||
'timeBetweenJokes' : 1000 | ||
}; | ||
|
||
|
||
// Function that prints a random joke | ||
app.printAJoke = function(){ | ||
|
||
// Get all the jokes | ||
var allJokes = jokesLib.allJokes(); | ||
|
||
// Get the length of the jokes | ||
var numberOfJokes = allJokes.length; | ||
|
||
// Pick a random number between 1 and the number of jokes | ||
var randomNumber = mathLib.getRandomNumber(1,numberOfJokes); | ||
|
||
// Get the joke at that position in the array (minus one) | ||
var selectedJoke = allJokes[randomNumber - 1]; | ||
|
||
// Send the joke to the console | ||
console.log(selectedJoke); | ||
}; | ||
|
||
|
||
// Function that loops indefinitely, calling the printAJoke function as it goes | ||
app.indefiniteLoop = function(){ | ||
|
||
// Create the interval, using the config variable defined above | ||
setInterval(app.printAJoke,app.config.timeBetweenJokes); | ||
}; | ||
|
||
|
||
// Invoke the loop | ||
app.indefiniteLoop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Title: Jokes Library | ||
* Description: Utility library for getting a list of Jokes | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// Dependencies | ||
var fs = require('fs'); | ||
|
||
// App object | ||
var jokes = {}; | ||
|
||
// Get all the jokes and return them to the user | ||
jokes.allJokes = function(){ | ||
|
||
// Read the text file containing the jokes | ||
var fileContents = fs.readFileSync(__dirname+'/jokes.txt', 'utf8'); | ||
|
||
// Turn the string into an array | ||
var arrayOfJokes = fileContents.split(/\r?\n/); | ||
|
||
// Return the array | ||
return arrayOfJokes; | ||
|
||
}; | ||
|
||
// Export the library | ||
module.exports = jokes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Why should you avoid the duck psychologist? He's a quack. | ||
What do cows do in their spare time? Go to the moooovies. | ||
Why do birds hate Instagram? They prefer to tweet. | ||
Where do fish keep all their money? The river bank. | ||
Why did the sheep get detention? It was baaaaaad. | ||
Why couldn't the horse congress pass any bills? They all kept voting naaaaaay. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Title: Math Library | ||
* Description: Utility library for math-related functions | ||
* Author: Leslie Lewis | ||
* Date: 10/24/17 | ||
* | ||
*/ | ||
|
||
|
||
// App object | ||
var math = {}; | ||
|
||
// Get a random integer between two integers | ||
// Inspired by: http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript | ||
math.getRandomNumber = function(min,max){ | ||
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0; | ||
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0; | ||
return Math.floor(Math.random()*(max-min+1)+min); | ||
}; | ||
|
||
|
||
// Export the library | ||
module.exports = math; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "basicExample", | ||
"version": "0.0.1", | ||
"license": "UNLICENSED", | ||
"private": "true", | ||
"description": "Simple file that declares a few functions and invokes them", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"jokes" : "0.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Create and export configuration variables | ||
* | ||
*/ | ||
|
||
// Container for all environments | ||
var environments = {}; | ||
|
||
// Staging (default) environment | ||
environments.staging = { | ||
'port' : 3000, | ||
'envName' : 'staging' | ||
}; | ||
|
||
// Production environment | ||
environments.production = { | ||
'port' : 5000, | ||
'envName' : 'production' | ||
}; | ||
|
||
// Determine which environment was passed as a command-line argument | ||
var currentEnvironment = typeof(process.env.NODE_ENV) == 'string' ? process.env.NODE_ENV.toLowerCase() : ''; | ||
|
||
// Check that the current environment is one of the environments above, if not default to staging | ||
var environmentToExport = typeof(environments[currentEnvironment]) == 'object' ? environments[currentEnvironment] : environments.staging; | ||
|
||
// Export the module | ||
module.exports = environmentToExport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Primary file for API | ||
* | ||
*/ | ||
|
||
// Dependencies | ||
var http = require('http'); | ||
var url = require('url'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var config = require('./config'); | ||
|
||
// Configure the server to respond to all requests with a string | ||
var server = http.createServer(function(req,res){ | ||
|
||
// Parse the url | ||
var parsedUrl = url.parse(req.url, true); | ||
|
||
// Get the path | ||
var path = parsedUrl.pathname; | ||
var trimmedPath = path.replace(/^\/+|\/+$/g, ''); | ||
|
||
// Get the query string as an object | ||
var queryStringObject = parsedUrl.query; | ||
|
||
// Get the HTTP method | ||
var method = req.method.toLowerCase(); | ||
|
||
//Get the headers as an object | ||
var headers = req.headers; | ||
|
||
// Get the payload,if any | ||
var decoder = new StringDecoder('utf-8'); | ||
var buffer = ''; | ||
req.on('data', function(data) { | ||
buffer += decoder.write(data); | ||
}); | ||
req.on('end', function() { | ||
buffer += decoder.end(); | ||
|
||
// Check the router for a matching path for a handler. If one is not found, use the notFound handler instead. | ||
var chosenHandler = typeof(router[trimmedPath]) !== 'undefined' ? router[trimmedPath] : handlers.notFound; | ||
|
||
// Construct the data object to send to the handler | ||
var data = { | ||
'trimmedPath' : trimmedPath, | ||
'queryStringObject' : queryStringObject, | ||
'method' : method, | ||
'headers' : headers, | ||
'payload' : buffer | ||
}; | ||
|
||
// Route the request to the handler specified in the router | ||
chosenHandler(data,function(statusCode,payload){ | ||
|
||
// Use the status code returned from the handler, or set the default status code to 200 | ||
statusCode = typeof(statusCode) == 'number' ? statusCode : 200; | ||
|
||
// Use the payload returned from the handler, or set the default payload to an empty object | ||
payload = typeof(payload) == 'object'? payload : {}; | ||
|
||
// Convert the payload to a string | ||
var payloadString = JSON.stringify(payload); | ||
|
||
// Return the response | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.writeHead(statusCode); | ||
res.end(payloadString); | ||
console.log("Returning this response: ",statusCode,payloadString); | ||
|
||
}); | ||
|
||
}); | ||
}); | ||
|
||
// Start the server | ||
server.listen(config.port,function(){ | ||
console.log('The server is up and running on port '+config.port+' in '+config.envName+' mode.'); | ||
}); | ||
|
||
// Define all the handlers | ||
var handlers = {}; | ||
|
||
// Sample handler | ||
handlers.sample = function(data,callback){ | ||
callback(406,{'name':'sample handler'}); | ||
}; | ||
|
||
// Not found handler | ||
handlers.notFound = function(data,callback){ | ||
callback(404); | ||
}; | ||
|
||
// Define the request router | ||
var router = { | ||
'sample' : handlers.sample | ||
}; |
Oops, something went wrong.