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:
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.
var fs = require(‘fs’);
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);
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’); }); }); }
req.on(‘err’, (err) => { console.log(err.message); });
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.
var http = require(‘http’);
var server = http.createServer((req, res) => { step 3 });
res.writeHead(200, {‘Content-Type’: ‘text/plain’}); res.end(‘Hello world’);
server.listen(3000); console.log(‘Server is listening on port: 3000’);
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.
http.createServer((req,rest) => { console.log(`${req.method} request for ${req.url}`); }).listen(3000); console.log("File server running on port 3000");
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. }
else { res.writeHead(404, {"Content-Type": "text/plain"}); res.end("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>
var http = require("http"); var fs = require("fs"); http.createServer( (req, res) => { … }).listen(3000); console.log("Form server listening on port 3000");
if (req.method === "GET") { … } else if (req.method === "POST") { … }
res.writeHead(200, {"Content-Type": "text/html"}); fs.createReadStream("./default.html", "UTF-8").pipe(res);
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> `); });
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
JavaScript is a dynamic computer programming language for the web. Jav...
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *