Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 2.95 KB

web_server.md

File metadata and controls

61 lines (47 loc) · 2.95 KB

Creating a Simple Web Server in Node.js

Overview

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.

The HTTP Module

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.

Creating a Web Server

Here’s a step-by-step guide on setting up a simple web server:

Step 1: Initialize a New Node.js Project

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

Step 2: Create the Server File

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}/`);
});

Step 3: Run the Server

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.

Testing the Server

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.

Understanding the Code

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.

Conclusion

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.