Creating an HTTP server in NodeJs

Why is a server needed in the first place?

Servers are needed to allow communication between two computers. These computers can be anything let's take an example of a client and services.

A server is a computer program or a piece of hardware that provides services to other computer programs or devices on a network. In web development, a server is responsible for receiving incoming requests from clients, processing those requests, and sending back the appropriate response. This allows clients, such as web browsers, to access and interact with web applications and websites.

What is HTTP?

HTTP stands for HyperText Transfer Protocol. As the name suggests it is a protocol, a protocol for communication. But HTTP is not the only protocol available there are many as TCP/IP FTP UDP etc. There are so many because different types of communication require different standards to ensure effective and efficient communication.

HTTP is a stateless protocol, which means that each request is processed independently of any previous requests. To maintain the state between requests, web applications often use cookies, which are small pieces of data that are stored on the client's computer and sent back to the server with subsequent requests.

Let's Make the Server

// Require the HTTP module const http = require('http');

// Define the port number to listen on const PORT = 8080;

// Create the HTTP server

const server = http.createServer((req, res) => {

res.setHeader('Content-Type', 'text/plain');

res.setHeader('X-Powered-By', 'Node.js');

// Write the response res.write('Hello, World!'); res.end(); });

// Start listening for incoming requests server.listen(PORT, () => { console.log(Server listening on port ${PORT}); });

Detailed Explanation

In this example, we first require the built-in http module, which provides the functionality to create an HTTP server.

Next, we define the port number that the server should listen on. In this case, we've set it to 8080, but you can choose any available port that you like.

We then create the server by calling the createServer method of the http module. This method takes a callback function that will be called every time a request is received. The function takes two arguments, req and res, which represent the incoming request and the outgoing response, respectively.

Inside the callback function, we set the response headers using the setHeader method of the res object. We've set the Content-Type header to text/plain and the X-Powered-By header to Node.js.

We then write the response using the write method of the res object, which writes the string "Hello, World!" to the response body. Finally, we end the response using the end method of the res object.

Finally, we start listening for incoming requests by calling the listen method of the server object, passing in the port number and a callback function that will be called when the server starts listening. In this example, we've simply logged a message to the console.