You are on page 1of 1

//if the function is simply returning a value, the curly brackets and the fs.readFile('index.

dFile('index.html', 'utf8', function (errors, contents){ Node Modules


return statement is optional response.writeHead(200, {'Content-Type': 'text/html'}); // send To include a module, use the require() function with the name of the module
abc = (a) => a+100; data about response Requiring a Node module allows you to use the module.exports object of another
//the parenthesis is even optional! response.write(contents); // send response body file!
abc = a => a+100; response.end(); // finished! my_module.js file:
}); module.exports = {
var obj = { } greet: function(){
name: 'coding', // request didn't match anything: console.log("we are now using a module!");
b: (word) => { else { },
console.log(this.name, word); response.writeHead(404); add: function(num1, num2){
} response.end('File not found!!!'); console.log("the sum is:", num1 + num2);
} } }
obj.b('is fun'); }); }
// tell your server which port to run on app.js file:
Asynchronous Callbacks functions running in parallel with other functions server.listen(6789); var my_module = require('./my_module');
let a = (function task1() { // print to terminal window my_module.greet();
function abc() { console.log("Running in localhost at port 6789"); my_module.add(5,6);
console.log('hello');
return 100; var server = http.createServer(function (request, response){...} require() method looks for the modules located in a folder called node_modules.
} To tell require() to look in the current directory (i.e. the folder that the file is
abc(); Configuring the root route: located in) we have to include "./" in front of the file path. "./" (dot-slash) is the
console.log('world'); if(request.url === '/') { file path for the current directory
return 5; fs.readFile('index.html', 'utf8', function (errors, contents){ my_module.js file:
})(); response.writeHead(200, {'Content-Type': 'text/html'}); module.exports = function(){
console.log(a); response.write(contents); return {
response.end(); greet: function(){
let a = (function() { }); console.log("we are now using a module!");
return function(a, b) { } },
console.log('hello'); add: function(num1, num2){
return a*b; 1. Notice the code begins with an if statement. We are asking if the URL console.log("the sum is:", num1 + num2);
} property of the request object is equal to "/". The route "/" is always }
console.log('world'); considered the root route. In English, we are asking if the URL requested by }
})(); the client is of a particular form. }
console.log(a(3,5)); 2. If the request URL matches the string to the right of the triple equals sign, we app.js file:
will serve the appropriate response. That response begins at fs.readFile(...). var my_module = require('./my_module')(); //notice the extra
Promise a JavaScript object that links producing code and consuming code This command goes and finds a file by name and reads it. invocation parentheses!
3. The name of the file we're opening is called 'index.html'. console.log(my_module);
Async & Await inside an async function, you can use the await keyword before a 4. The second parameter is the encoding of the file. Here, we're telling my_module.greet();
the fs object what type of characters to expect in the file it's opening. You
call to a function that returns a promise. my_module.add(5,6);
will need to include this line for any text-based document you serve,
async makes a function return a Promise
remember this!!
await makes a function wait for a Promise
5. When the fs module reads the file, it passes the contents of the file into
a callback and this is where we actually formulate and serve the response.
6. Notice the first thing we do is call the response.writeHead() method. This
FS and HTTP method sends the headers for our response along with a status code.
// get the http module: A header is the part of a response that contains the specifics of the response.
var http = require('http'); We need to tell the browser what type of response we're serving. The status
// fs module allows us to read and write content for responses!! code is a code that tells the browser the status of the response. Any status
var fs = require('fs'); code in the 200's or 300's is good. Anything in the 400's to the 500's is bad.
// creating a server using http module: For now, just always put a 200 as your status code on any valid request.
var server = http.createServer(function (request, response){ 7. After all of that, we finally send the response to the client using
// see what URL the clients are requesting: the response.write() method, which just sends the contents of the files to
console.log('client request URL: ', request.url); the client.
// this is how we do routing: 8. Since a response might contain multiple chunks of data, we
if(request.url === '/') { call response.end() when we are finished.

You might also like