Table of contents
1.
Introduction:
2.
Creating the Server:
3.
Example
4.
Frequently Asked Questions:
5.
Key Takeaways:
Last Updated: Mar 27, 2024

HTTP Server using Node.js

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction:

An HTTP server is used to open a file in the server and send the requested content to the user.  We can create an HTTP Server using Node.js is an open-source backend JavaScript runtime environment. 

To know more about HTTP, check out the blog What is HTTP?

This article will learn about creating an HTTP server using Node.js that gets information from the server port and sends it to the clients.

To know more about Node.js checkout the blog, "Introduction to Node.js."

Creating the Server:

Let us start building our HTTP server using Node.js:

  1. The first step is to include the node.js HTTP module
const http = require("http");
You can also try this code with Online Javascript Compiler
Run Code

 

2. Our server will have a host and a port. Let us define both:

const host = 'localhost';
const port = 8000;
You can also try this code with Online Javascript Compiler
Run Code

 

Localhost is a special private address that refers to the computer itself. It refers to the internal IP address of 127.0.0.1, which is only available to the local computer. The port is the door to the IP address. We have used port number 8000 in our code.

 

3. We will now create a request listener function. It takes two arguments: Request Object (req) and Response Object (res). The request object contains things such as the requested URL. The response object is used to send headers and content of the response back to the user.

const requestListener = function (req, res) {
   // Setting status code for the response
   res.writeHead(200);
   res.end("My first server!");
};
You can also try this code with Online Javascript Compiler
Run Code

 

This function returns the data that the server has to return. In this case, we returned "My first server!" as the response to the client. 

 

If the result is a JSON file, then a header should be added to our HTTP server using  Node.js to specify so.

const requestListener = function (req, res) {
   // Header
   res.setHeader("Content-Type", "application/json");
   res.writeHead(200);
   res.end(`{"message": "This is a JSON response"}`);
};
You can also try this code with Online Javascript Compiler
Run Code

 

For specifying the response time as CSV (comma-seperated values) in our HTTP server using Node.js.

const requestListener = function (req, res) {
   // Header
   res.setHeader("Content-Type", "text/csv");
   res.writeHead(200);
   res.setHeader("Content-Disposition", "attachment;filename=sample.csv");
};
You can also try this code with Online Javascript Compiler
Run Code

 

For specifying the response type as HTML in our HTTP server using Node.js.:

const requestListener = function (req, res) {
   // Header
   res.setHeader("Content-Type", "text/html");
   res.writeHead(200);
   res.end(`<html><body><h1>This is HTML</h1></body></html>`);
};
You can also try this code with Online Javascript Compiler
Run Code

 

4. The final step is creating the server.

const server = http.createServer(requestListener);
server.listen(port);
You can also try this code with Online Javascript Compiler
Run Code

 

This createServer method calls the requestListener function whenever a request is received and we return a status code 200, which signals a successful response, along with the body.

The listen method asks the server to wait at the specified port.

 

In these steps, we can create a simple HTTP server using Node.js. We can also add files to this function and modify them according to our needs.

Example

Let's try running the first requestListener function. Save the file and run it from the terminal. Now, try opening up localhost:8080 on any browser.  If everything has been set up correctly, you should see your server saying My first server!

 

Command:

node main.js

 

Output

 

Frequently Asked Questions:

  1. Which method is used to create an HTTP Server in Node.js?

Ans-> We use the require() method to include the HTTP module to create the server.

2. What is the default port for HTTPS and HTTP?

Ans-> The default port for HTTPS is 443, and HTTP is 80.

3. What is the 100 Continue response code in HTTP?

Ans-> It is used to tell the client that he should continue with his request. It informs the client that the initial response to the request has been received.

4. Why do we use the Node.js framework to make a webserver?

Ans-> HTTP module is low-level and time-consuming. Hence we use frameworks like express( Node.js web application framework) and restify(( Node.js web service framework) to make the server.

 

Key Takeaways:

In this article, we learned how to create an HTTP Server using Node.js. Readers, if you liked this blog, you can learn more about Node.js from here

 

If you are preparing for your next web development interview, check out the blogs, 25 CSS Interview Questions, and 30 JavaScript interview questions.

Live masterclass