How to create an HTTP server in Nodejs

Felix Olufade
2 min readApr 12, 2021

story by (Kristoff-TM-Eureka) Christopher Egbaaibon

When you view a webpage, you are making a request to another computer on the internet, which then provides you the webpage as a response. That computer you are talking to via the Internet is a web server.
A web server receives HTTP requests from a client, like your browser and provides HTTP response like an HTML or JSON from an APl.
Creating an HTTP requires a lot of software. This software is divided into to components; the Front-end and the Back-end. Front-end code is mainly concerned with how the content is presented i.e the text styling, color, navigation bar, etc, while the Back-end code is concerned with how the exchange of data, processing and storage takes place.
The code that handles network requests from the browser or communicates with the database is primarily managed by Back-end code. The basic functionality of the required function is that it reads JavaScript file, executes the file, and then proceeds to return the export objects. So in this case, since we want to use the HTTP Module, we will use the required function to get the desired functions from the HTTP Module, so that it can be used in Our application. In this line of code, the creation of a server application which is based on a simple function is needed. This function is called whenever a request is made into our server application.
However, when a request is received, a response with a header typed “200” is sent, this is the normal response which is sent in an HTTP header when a successful response is sent to the client. In the response itself, we are sending the string “Hello World”. Then the usage of the server’s listen function takes place to make our server application listen to client request or port number 7000, you can specify any available port here.
The HTTP which is the HyperText Transfer Protocol was built by Node.js to transfer data. It can create an HTTP server that listens to server ports and gives a response back.
Making a simple HTTP server in Node.js has become the de facto “Hello World” for the platform. On the one hand, Node.js provides extremely easy to use HTTP APl. A simple web server also serves as an excellent demonstration of the asynchronous strengths of Node.js. A function that takes request objects and response objects as parameters contain request URL, response objects is how we send the header and contents back to the user making the request. Here we return a 200 response code with the body “Hello World”, other headers will also be set here.
In summary, the method creates a server that is called ‘request listener’ whenever a request comes in. The next line server listener 8080 called the listen method which causes the server to wait for incoming requests on the specified port 8080 in this case. And there you have your basic Node.js HTTP server.

--

--