How to Use the HTTP Module to Create a Web Server in Node.js

This guide will teach you how to build a basic HTTP server using the Node.js http module.

Irene mmassy
4 min readJul 9, 2022

Using JavaScript, Node JS is a platform for creating scalable and quick server-side applications.

You submit a request to a different computer on the internet when you view a webpage in your browser, and that computer responds by giving you the webpage. It is a web server that you are communicating with online. Using a client like your browser, a web server takes HTTP requests and responds with an HTTP response, such as an HTML page or JSON from an API.

The HTTP built-in module of Node.js is dependable and works with the NPM ecosystem.

Prerequisites

Ensure that Node.js is installed on your development machine.

Step 1: Constructing a Simple HTTP Server

The built-in HTTP module in Node.js enables data transfer over the HyperText Transfer Protocol (HTTP).

To include the HTTP module, use the require() method:

const http = require(‘http’);

Step 2 : Create Server

The HTTP module has the ability to build an HTTP server that listens on server ports and responds to clients.

All of the information from the incoming HTTP request is captured by the request object. For the server, HTTP answers are returned using the response object.

Use the createServer() method to create an HTTP server:

#Example

const server = http.createServer((req, res) => {res.writeHead(200);res.end("we are at the server");});

Step 3 : Prepare response

After building our server, we need to assign it a network address. With the server. Listen() , It takes three arguments: a callback function that is activated when the server starts to listen, a port, and a host.

Although it is a good idea to clearly declare which host and port a web server should use, each of these parameters is optional.

const server = http.createServer((req, res) => {res.write(‘Node.js says hello!’); //write a response to the clientres.end(‘server is working fine’);});server.listen(port, host, () => {console.log(`listening at port ${host}:${port}`);});

Step 4 : Run the Web Server

When you run node server.js with this saved in a file called server.js, your program will hang because it is awaiting connections to respond to. If you want to see it respond, you must connect to it.

Open a browser and type localhost:3000 into the address bar to see whether it works. Your server should display “server is working fine” if everything has been configured properly.

node server.js

Step 5 : Test the Web Server

Open a browser and hit the URL, “http://127.0.0.1:3000/”, to trigger a request to our Web Server.

The value localhost is a special private address that computers use to refer to themselves. It usually corresponds to the internal IP address 127.0.0.1 and is exclusively accessible to the local computer; neither any local networks we have joined nor the internet have access to it.

The port is a designation used by servers as a “door” or endpoint to our IP address. For our example, our web server is on port 3000. In most circumstances, developers will utilize ports 8080 and 8000 instead of other ports for HTTP servers since they are the standard ports used in development.

When we bind our server to this host and port, we will be able to reach our server by visiting http://localhost:3000 in a local browser.

Conclusion

The entire server-creation code

 const http = require('http');const port = 3000;const host ='27.0.0.1.1';const server = http.createServer((req, res) => {//write a response to the clientres.end('server is working fine');});server.listen(port, host, () => {console.log(`listening at port ${host}:${port}`);});
  • The ‘http’ module of the Node.js framework can be used to create web servers. When a client makes a request of the application, it can be configured to listen on a specific port and respond to the client.
  • You can obtain information from websites using the “request” module. The data would include all of the information on the web page that was requested from the appropriate website.

--

--