10X Sale
kh logo
All Courses
  1. Tutorials
  2. Web Development

HTTP Module

Updated on Aug 29, 2025
 
14,484 Views

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:

  1. http: https://nodejs.org/api/http.html
  2. 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.

  1. var http = require(‘http’);
  2. var fs = require(‘fs’);
  3. var path = require(‘path’);
  4. 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 with an 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");

The 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 case of the GET method, 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);

- With the 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:

  1. Your Form Results
  2. first=firstName&last=lastName&email=firstName.lastName@gmail.com
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses