HTTP

Learn how to make a web server with the HTTP module in Node.js.

We'll cover the following

The web

Websites, much like this one, don’t use a command-line terminal to serve all those beautiful web pages. Simple servers and clients that work using the terminal exist on the transport layer, whereas HTTP resides on the application layer of the internet protocol suite. . This is the layer users interact with using a website’s front-end. Node.js allows us to serve web pages using the http module. Let’s see how a simple server can be made in just a few lines of code:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3500;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/html');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
Hit the RUN button to view the output

The server-side

  • We import the http module on line 1.
  • We declare our hostname and port on line 3 and line 4.
  • The createServer method on line 6 creates an http.Server instance. A requestListener function is also added as a callback here. The requestListener is automatically addedwill be fired when a request event is raised to the request event. The request event returns two objects: a request and a response. The request event is automatically fired when a client requests something from the server. In our case, the client simply requests for the web page to load.
  • We use req and res as the names for the request and response returned by the request event. The request is an instance of the http.IncomingMessage class, and the response is an instance of the http.ServerResponse class. We do not use the req for this simple demo; instead, we focus on the res.
  • The statusCode method on line 7 sets the HTTP status code. We set it to 200. If you are not familiar with HTTP status codes, 200 translates to OK. You might be familiar with 404, which translates to Not Found.
  • HTTP headers let the client and the server pass additional information with an HTTP request or response. There is a standard for these headers; we will use the Content-Type header and set it to text/html on line 8.
  • The end method tells the server that all of the response headers and body have been sent. The server now knows that the message is complete. This method must be called on each response. We pass it our data, Hello World as the only parameter; however, we can also pass it a callback function that will run after the response ends. We will see how that works in a later lesson.
  • The listen() method is identical to the server.listen() method which is used in the net module. Now, the server can start accepting requests from clients. We are also using a callback function to output the hostname and the port on the console.

Web servers are much more complex than the one we made in the code widget above. Ideally, for a lot of this code, we have libraries and modules that make building web applications a lot easier.

Get hands-on with 1300+ tech skills courses.