Node.js makes it straightforward to build web servers due to its asynchronous, event-driven architecture. This guide will show you how to create a basic web server using the native HTTP module provided by Node.js, which can handle HTTP requests and send responses back to clients.
Node.js includes an HTTP module right out of the box, which can create an HTTP server listening for requests on a given port. This module provides a way of making Node.js transfer data over HTTP with ease.
Here’s a step-by-step guide on setting up a simple web server:
First, create a new directory for your project and initialize it with a package.json
file:
mkdir my-web-server
cd my-web-server
npm init -y
Create a file named server.js
in your project directory:
const http = require('http');
// Define the server port and IP
const hostname = '127.0.0.1';
const port = 3000;
// Create an HTTP server that listens to server ports and gives a response back to the client
const server = http.createServer((req, res) => {
res.statusCode = 200; // HTTP status code 200 OK
res.setHeader('Content-Type', 'text/plain'); // Set the header content-type
res.end('Hello World\n'); // Send the response body "Hello World"
});
// Start the server
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
To start the server, run the following command in your terminal:
node server.js
This will start the server on localhost at port 3000. You can access the server in your web browser by navigating to http://127.0.0.1:3000
.
Open your preferred web browser and visit http://127.0.0.1:3000
. You should see the text "Hello World" displayed in the browser, which confirms that the server is running correctly and able to handle HTTP requests and responses.
The http.createServer()
method turns your computer into an HTTP server. The createServer()
method creates an HTTP Server object. The function passed into http.createServer()
is a callback that gets executed each time someone tries to access the server.
req
(request) argument represents the request from the client, as an object (http.IncomingMessage object).res
(response) argument is used to send back a response to the client.
Building a web server with Node.js using the HTTP module is simple and only requires a few lines of code. This server can be extended to serve HTML pages, handle different types of requests, and send various responses, forming the backbone of a basic web application.
For more advanced features and capabilities, consider exploring frameworks like Express.js, which provide additional functionalities to simplify the development of complex applications.