top
upGrad KnowledgeHut SkillFest Sale!

Search

Node JS Tutorial

In this topic, we are going to understand the HTTP module. It is used for making a web server to handle requests and provide responses. There are two modules mentioned below: http: https://nodejs.org/api/http.html https: https://nodejs.org/api/https.html Both these modules are similar but when we want to work on a secure module then we are going to use https. And for which we need to supply a secure certificate whereas for http this is not needed. Let's start using https module to make a request to some page say Wikipedia of Sachin Tendulkar who is known as the god of cricket. https://en.wikipedia.org/wiki/Sachin_Tendulkar. In this URL, the domain is en.wikipedia.org and route is wiki/sachin_tendulkar. App1:  Let's create a request.js file and import https module. var https = require(‘https’); // since Wikipedia is served in https, we are https. If it has to be another site, which serves http, we would have used http and the rest of the code remains the same. Also we need a fs module to stream the response to a file. var fs = require(‘fs’); To make a request we need to build options to it. In our case i.e. wikipedia var options = { hostname: ‘en.wikipedia.org’, port: 443, path: ‘/wiki/Sachin_Tendulkar’, method: ‘GET’ } Let’s make a http request with above options with a callback to accept response. var request = https.request(options, callback); Response is a stream object i.e. our Wikipedia page provided in binary format and lets listen to this stream object i.e. on data event and store it to file system. var callback = (res) => {  var responseBody = ‘’;  console.log(‘response from server started’);  console.log(`Server status: ${res.statusCode}`);  console.log(`Response headers: ${res.headers}`);  res.setEncoding(‘UTF-8’); // since data is in binary  res.on(‘data’, (chunk) => {  console.log(`--data length--${chunk.lenghth}`);  responseBody = += chunk;  });  res.on(‘end’,() => {  fs.writeFile(‘SachinTendulkar.html’, responseBody, (err) => {  if (err) { throw err;}  console.log(‘File is downloaded’);  });  });  } To see if there are any errors,  listen to error event on req object req.on(‘err’, (err) => {  console.log(err.message);  }); Finally end our request i.e. req.end(); Run node request.js and observe that new file is created with Sachin-Tendulkar.html varhttps = require("https");  varfs = require("fs");  varoptions = { hostname:"en.wikipedia.org", port:443, path:"/wiki/Sachin_Tendulkar", method:"GET" };  varreq = https.request(options, function(res) {  varresponseBody = "";  console.log("Response from server started.");  console.log(`Server Status: ${res.statusCode} `);  console.log("Response Headers: %j", res.headers);  res.setEncoding("UTF-8");  res.on("data", function(chunk) {  console.log(`--data length-- ${chunk.length}`);  responseBody += chunk;  });  res.on("end", function() {  fs.writeFile("Sachin-Tendulkar.html", responseBody, function(err) {  if (err) { throwerr; }  console.log("File Downloaded");  });  });  });  req.on("error", function(err) {  console.log(err.message);  });  req.end(); App2: Let's build a web server but this time with http module. So we don’t need a secure certificate. Create a server.js file and import http  var http = require(‘http’); Let’s create a server using http  var server = http.createServer((req, res) => {  step 3  }); Here we are going write headers to the response and end on completion. res.writeHead(200, {‘Content-Type’: ‘text/plain’});  res.end(‘Hello world’); Finally, we need to set a port on which we can listen and log serve has started listening server.listen(3000);  console.log(‘Server is listening on port: 3000’); Now run node server and see that server is listening on port 3000 Try to navigate to localhost:3000 and see how it responds with the plain text ‘Hello world’. We can further extend this by writing an HTML content and changing the header to text/html. Also, we can find the request URL and request method from the request object. Based on URL and method we can response accordingly. Note: If we want to build an https server, we need to provide options to createServer as shown below: const options = {  key: fs.readFileSync('./agent2-key.pem'),  cert: fs.readFileSync('./agent2-cert.pem')  }  https.createServer(options,(req, res)=>{…});  or   const options ={    pfx: fs.readFileSync('./test_cert.pfx'),    passphrase:'sample'  }; App3: Now build a server to serve files. Let's create a fileserver.js and create a folder ‘public’ with few files in it. Open fileserver file and import the following modules. var http = require(‘http’); var fs  = require(‘fs’); var path = require(‘path’); create a http server and listen to 3000 port http.createServer((req,rest) => {  console.log(`${req.method} request for ${req.url}`);  }).listen(3000);   console.log("File server running on port 3000"); Now based on the url, we will serve the files inside the public folder i.e default page (may be previous sachin-tendulkar.html page downloaded) or image (windows provide default pictures copy one to the public folder say Jellyfish.jpg). If (req.url === ‘/’) {  fs.readFile("./public/default.html", "UTF-8", function(err, html) {  res.writeHead(200, {"Content-Type": "text/html"});  res.end(html);  });  // refer to file system topic to understand this better  } else if (req.url.match(/.jpg$/)) {  var imgPath = path.join(__dirname, 'public', req.url);  var imgStream = fs.createReadStream(imgPath);  res.writeHead(200, {"Content-Type": "image/jpeg"});  imgStream.pipe(res);  // refer to stream topic.  } For any other page, lets respond invalid request else {  res.writeHead(404, {"Content-Type": "text/plain"});  res.end("404 Not Found");  } Run node fileserver and navigate to localhost:3000 and observe the sachin-tendulkar page is rendered. If we navigate to localhost:3000/jellyfish.jpg then it should return the image. For other page url then we get 404 Not Found. varhttp = require("http");  varfs = require("fs");  varpath = require("path");  http.createServer(function(req, res) {  console.log(`${req.method} request for ${req.url}`);  if (req.url === "/") {  fs.readFile("./public/default.html", "UTF-8", function(err, html) {  res.writeHead(200, {"Content-Type":"text/html"});  res.end(html);  });  } elseif (req.url.match(/.jpg$/)) {  varimgPath = path.join(__dirname, 'public', req.url);  varimgStream = fs.createReadStream(imgPath);  res.writeHead(200, {"Content-Type":"image/jpeg"});  imgStream.pipe(res);  } else {  res.writeHead(404, {"Content-Type":"text/plain"});  res.end("404 Not Found");  }  }).listen(3000);  console.log("File server running on port 3000"); Above example can be extended to serve JSON as content type based on URL as shown below: res.writeHead(200, {"Content-Type": "text/json"});  res.end(JSON.stringify(data)) Here data could be content of the file with JSON extension. App4:  All the above examples are serving ‘GET’ method. Lets extend it further to serve a FORM this time. Inside this form, we will have submit button and on click of which will do a ‘POST’ method to our server. <!DOCTYPE html>  <html>  <head>      <meta charset="utf-8">      <title>Fill out this Form</title>  </head>  <body>      <h 1>Fill out this form</h 1>      <form action="/" method="post">          <label for="first">First name</label>          <input type="text" id="first" name="first" required />          <label for="last">Last name</label>          <input type="text" id="last" name="last" required />          <label for="email">Email</label>          <input type="text" id="email" name="email" required />          <button>Submit</button>      </form>  </body>  </html> Create a file formserver.js and start a server on port 3000. var http = require("http");  var fs = require("fs");  http.createServer( (req, res) => {  …   }).listen(3000);  console.log("Form server listening on port 3000"); Let’s add conditional code based on request method ‘GET’ and ‘POST’. if (req.method === "GET") {  …  } else if (req.method === "POST") {  …  } In GET method case, lets respond with the default.html containing FORM mentioned above. res.writeHead(200, {"Content-Type": "text/html"});  fs.createReadStream("./default.html", "UTF-8").pipe(res); In POST method, listen to data event and end event on request object to capture user data and respond. var body = "";  req.on("data", (chunk) => {  body += chunk;  });  req.on("end", function() {  res.writeHead(200, {"Content-Type": "text/html"});  res.end(`  <!DOCTYPE html>  <html>  <head>  <title>Form Results</title>  </head>  <body>  <h 1>Your Form Results</h 1>  <p>${body}</p>  </body>  </html>  `);  }); Run node formserver and see terminal output ‘Form server listening on port 3000’ Navigate to localhost:3000 and see the form is rendered. Fill the details and click on submit button and observe that data is captured. varhttp = require("http");  varfs = require("fs");  http.createServer(function(req, res) {  if (req.method === "GET") {  res.writeHead(200, {"Content-Type":"text/html"});  fs.createReadStream("./default.html", "UTF-8").pipe(res);  } elseif (req.method === "POST") {  varbody = "";  req.on("data", function(chunk) {  body += chunk;  });  req.on("end", function() {  res.writeHead(200, {"Content-Type":"text/html"});  res.end(`  <!DOCTYPE html>  <html>  <head>  <title>Form Results</title>  </head>  <body>  <h 1>Your Form Results</h 1>  <p>${body}</p>  </body>  </html>  `);  });  }  }).listen(3000);  console.log("Form server listening on port 3000"); On post form submission: Your Form Results first=firstName&last=lastName&email=firstName.lastName@gmail.c
logo

Node JS Tutorial

HTTP Module

In this topic, we are going to understand the HTTP module. It is used for making a web server to handle requests and provide responses. There are two modules mentioned below: 

http: https://nodejs.org/api/http.html 

https: https://nodejs.org/api/https.html 

Both these modules are similar but when we want to work on a secure module then we are going to use https. And for which we need to supply a secure certificate whereas for http this is not needed. 

Let's start using https module to make a request to some page say Wikipedia of Sachin Tendulkar who is known as the god of cricket. https://en.wikipedia.org/wiki/Sachin_Tendulkar

In this URL, the domain is en.wikipedia.org and route is wiki/sachin_tendulkar. 

App1:  

  • Let's create a request.js file and import https module. 
var https = require(‘https’); 

// since Wikipedia is served in https, we are https. If it has to be another site, which serves http, we would have used http and the rest of the code remains the same. 

  • Also we need a fs module to stream the response to a file. 
var fs = require(‘fs’); 
  • To make a request we need to build options to it. In our case i.e. wikipedia 
var options = { hostname: ‘en.wikipedia.org’, port: 443, path: ‘/wiki/Sachin_Tendulkar’, method: ‘GET’ } 
  • Let’s make a http request with above options with a callback to accept response. 
var request = https.request(options, callback); 
  • Response is a stream object i.e. our Wikipedia page provided in binary format and lets listen to this stream object i.e. on data event and store it to file system. 
var callback = (res) => { 
var responseBody = ‘’; 
console.log(‘response from server started’); 
console.log(`Server status: ${res.statusCode}`); 
console.log(`Response headers: ${res.headers}`); 
res.setEncoding(‘UTF-8’); // since data is in binary 
res.on(‘data’, (chunk) => { 
console.log(`--data length--${chunk.lenghth}`); 
responseBody = += chunk; 
}); 
res.on(‘end’,() => { 
fs.writeFile(‘SachinTendulkar.html’, responseBody, (err) => { 
if (err) { throw err;} 
console.log(‘File is downloaded’); 
}); 
}); 
} 
  • To see if there are any errors,  listen to error event on req object 
req.on(‘err’, (err) => { 
console.log(err.message); 
}); 
  • Finally end our request i.e. req.end(); 
  • Run node request.js and observe that new file is created with Sachin-Tendulkar.html 
varhttps = require("https"); 
varfs = require("fs"); 
varoptions = { hostname:"en.wikipedia.org", port:443,  path:"/wiki/Sachin_Tendulkar", method:"GET" }; 
varreq = https.request(options, function(res) { 
varresponseBody = ""; 
console.log("Response from server started."); 
console.log(`Server Status: ${res.statusCode} `); 
console.log("Response Headers: %j", res.headers); 
res.setEncoding("UTF-8"); 
res.on("data", function(chunk) { 
console.log(`--data length-- ${chunk.length}`); 
responseBody += chunk; 
  }); 
res.on("end", function() { 
fs.writeFile("Sachin-Tendulkar.html", responseBody, function(err) { 
if (err) { throwerr; } 
console.log("File Downloaded"); 
    }); 
  }); 
}); 
req.on("error", function(err) { 
console.log(err.message); 
}); 
req.end(); 

App2: Let's build a web server but this time with http module. So we don’t need a secure certificate. 

  • Create a server.js file and import http  
var http = require(‘http’); 
  • Let’s create a server using http  
var server = http.createServer((req, res) => { 
step 3 
}); 
  • Here we are going write headers to the response and end on completion. 
res.writeHead(200, {‘Content-Type’: ‘text/plain’}); 
res.end(‘Hello world’); 
  • Finally, we need to set a port on which we can listen and log serve has started listening 
server.listen(3000); 
console.log(‘Server is listening on port: 3000’); 
  • Now run node server and see that server is listening on port 3000 
  • Try to navigate to localhost:3000 and see how it responds with the plain text ‘Hello world’. 
  • We can further extend this by writing an HTML content and changing the header to text/html. Also, we can find the request URL and request method from the request object. Based on URL and method we can response accordingly. 

Note: If we want to build an https server, we need to provide options to createServer as shown below: 

const options = { 
key: fs.readFileSync('./agent2-key.pem'), 
cert: fs.readFileSync('./agent2-cert.pem') 
} 
https.createServer(options,(req, res)=>{…}); 
or  
const options ={ 
  pfx: fs.readFileSync('./test_cert.pfx'), 
  passphrase:'sample' 
}; 

App3: Now build a server to serve files. 

  • Let's create a fileserver.js and create a folder ‘public’ with few files in it. 
  • Open fileserver file and import the following modules. 
  • var http = require(‘http’); 
  • var fs  = require(‘fs’); 
  • var path = require(‘path’); 
  • create a http server and listen to 3000 port 
http.createServer((req,rest) => { 
console.log(`${req.method} request for ${req.url}`); 
}).listen(3000);  
console.log("File server running on port 3000"); 
  • Now based on the url, we will serve the files inside the public folder i.e default page (may be previous sachin-tendulkar.html page downloaded) or image (windows provide default pictures copy one to the public folder say Jellyfish.jpg). 
If (req.url === ‘/’) { 
fs.readFile("./public/default.html", "UTF-8", function(err, html) { 
res.writeHead(200, {"Content-Type": "text/html"}); 
res.end(html); 
}); 
// refer to file system topic to understand this better 
} else if (req.url.match(/.jpg$/)) { 
var imgPath = path.join(__dirname, 'public', req.url); 
var imgStream = fs.createReadStream(imgPath); 
res.writeHead(200, {"Content-Type": "image/jpeg"}); 
imgStream.pipe(res); 
// refer to stream topic. 
} 
  • For any other page, lets respond invalid request 
else { 
res.writeHead(404, {"Content-Type": "text/plain"}); 
res.end("404 Not Found"); 
} 
  • Run node fileserver and navigate to localhost:3000 and observe the sachin-tendulkar page is rendered. 
  • If we navigate to localhost:3000/jellyfish.jpg then it should return the image. For other page url then we get 404 Not Found. 
varhttp = require("http"); 
varfs = require("fs"); 
varpath = require("path"); 
http.createServer(function(req, res) { 
console.log(`${req.method} request for ${req.url}`); 
if (req.url === "/") { 
fs.readFile("./public/default.html", "UTF-8", function(err, html) { 
res.writeHead(200, {"Content-Type":"text/html"}); 
res.end(html); 
    }); 
  } elseif (req.url.match(/.jpg$/)) { 
varimgPath = path.join(__dirname, 'public', req.url); 
varimgStream = fs.createReadStream(imgPath); 
res.writeHead(200, {"Content-Type":"image/jpeg"}); 
imgStream.pipe(res); 
  } else { 
res.writeHead(404, {"Content-Type":"text/plain"}); 
res.end("404 Not Found"); 
  } 
}).listen(3000); 
console.log("File server running on port 3000"); 

Above example can be extended to serve JSON as content type based on URL as shown below: 

res.writeHead(200, {"Content-Type": "text/json"}); 
res.end(JSON.stringify(data)) 

Here data could be content of the file with JSON extension. 

App4:  

All the above examples are serving ‘GET’ method. Lets extend it further to serve a FORM this time. Inside this form, we will have submit button and on click of which will do a ‘POST’ method to our server. 

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Fill out this Form</title> 
</head> 
<body> 
    <h 1>Fill out this form</h 1> 
    <form action="/" method="post"> 
        <label for="first">First name</label> 
        <input type="text" id="first" name="first" required /> 
        <label for="last">Last name</label> 
        <input type="text" id="last" name="last" required /> 
        <label for="email">Email</label> 
        <input type="text" id="email" name="email" required /> 
        <button>Submit</button> 
    </form> 
</body> 
</html> 
  • Create a file formserver.js and start a server on port 3000. 
var http = require("http"); 
var fs = require("fs"); 
http.createServer( (req, res) => { 
…  
}).listen(3000); 
console.log("Form server listening on port 3000"); 
  • Let’s add conditional code based on request method ‘GET’ and ‘POST’. 
if (req.method === "GET") { 
… 
} else if (req.method === "POST") { 
… 
} 
  • In GET method case, lets respond with the default.html containing FORM mentioned above. 
res.writeHead(200, {"Content-Type": "text/html"}); 
fs.createReadStream("./default.html", "UTF-8").pipe(res); 
  • In POST method, listen to data event and end event on request object to capture user data and respond. 
var body = ""; 
req.on("data", (chunk) => { 
body += chunk; 
}); 
req.on("end", function() { 
res.writeHead(200, {"Content-Type": "text/html"}); 
res.end(` 
<!DOCTYPE html> 
<html> 
<head> 
<title>Form Results</title> 
</head> 
<body> 
<h 1>Your Form Results</h 1> 
<p>${body}</p> 
</body> 
</html> 
`); 
}); 
  1. Run node formserver and see terminal output ‘Form server listening on port 3000’ 
  2. Navigate to localhost:3000 and see the form is rendered. 
  3. Fill the details and click on submit button and observe that data is captured. 
varhttp = require("http"); 
varfs = require("fs"); 
http.createServer(function(req, res) { 
if (req.method === "GET") { 
res.writeHead(200, {"Content-Type":"text/html"}); 
fs.createReadStream("./default.html", "UTF-8").pipe(res); 
  } elseif (req.method === "POST") { 
varbody = ""; 
req.on("data", function(chunk) { 
body += chunk; 
    }); 
req.on("end", function() { 
res.writeHead(200, {"Content-Type":"text/html"}); 
res.end(` 
        <!DOCTYPE html> 
        <html> 
          <head> 
            <title>Form Results</title> 
          </head> 
          <body> 
            <h 1>Your Form Results</h 1> 
            <p>${body}</p> 
          </body> 
        </html> 
      `); 
    }); 
  } 
}).listen(3000); 
console.log("Form server listening on port 3000"); 

On post form submission: 

Your Form Results 

first=firstName&last=lastName&email=firstName.lastName@gmail.c

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. JavaScript was first known as LiveScript. Later on, Netscape changed its name to JavaScript because of its popularity and the excitement generated by it. JavaScript is lightweight and most commonly used as a part of web pages supported by most web browsers like Chrome, Internet Explorer, Opera, Safari, Edge, and Firefox.
JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. Jav...

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More