How to Build a Simple Web Server with Node.js ?
Last Updated :
14 Aug, 2024
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and it’s not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make a web server using node.js.
Creating Web Servers Using NodeJS: There are mainly two ways as follows.
Using Built-in HTTP module
HTTP and HTTPS, these two inbuilt modules are used to create a simple server. The HTTPS module provides the feature of the encryption of communication with the help of the secure layer feature of this module. Whereas the HTTP module doesn't provide the encryption of the data.
Approach
Building a simple Node.js web server with the http module by using http.createServer(), which listens for requests, sends responses, and is ideal for understanding core server functionality.
Project structure: It will look like this.
JavaScript
// Filename - index.js
// Importing the http module
const http = require("http")
// Creating server
const server = http.createServer((req, res) => {
// Sending the response
res.write("This is the response from the server")
res.end();
})
// Server listening to port 3000
server.listen((3000), () => {
console.log("Server is Running");
})
Run index.js file using below command:
node index.js

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Using Express Module
The express.js is one of the most powerful frameworks of the node.js that works on the upper layer of the http module. The main advantage of using express.js server is filtering the incoming requests by clients.
Approach
To create a web server with Express initialize an app with express(), defining routes with app.get(), and sending responses using res.send(). Express simplifies development with built-in features and middleware.
Installing module: Install the required module using the following command.
npm install express
Project structure: It will look like this.

Example:This example demonstrates creating a simple web server using express.js
JavaScript
// Filename - index.js
// Importing express module
const express = require("express")
const app = express()
// Handling GET / request
app.use("/", (req, res, next) => {
res.send("This is the express server")
})
// Handling GET /hello request
app.get("/hello", (req, res, next) => {
res.send("This is the hello response");
})
// Server setup
app.listen(3000, () => {
console.log("Server is Running")
})
Run the index.js file using the below command:
node index.js
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Summary
Creating a web server with Node.js can be done using the http module for a basic understanding or Express for more advanced features and ease of use. Both approaches highlight Node.js's versatility in handling server-side tasks, making it a powerful choice for web development.
Similar Reads
How to Build a Node.js Proxy Server ? A proxy server acts as an intermediary between a client and other servers. It can be used for various purposes, including load balancing, security, and caching. In the context of web development, a proxy server forwards requests from clients to other servers, fetches responses, and sends them back t
4 min read
How To Create a Simple HTTP Server in Node? NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers. What is HTTP?HTTP (Hypertext Transfer Protocol) is a protocol used for transferri
3 min read
How to Create HTTPS Server with Node.js ? Creating an HTTPS server in Node.js ensures secure communication between your server and clients. HTTPS encrypts data sent over the network, providing a layer of security essential for handling sensitive information. This guide will walk you through the process of setting up an HTTPS server in Node.
4 min read
How to Create a Simple Server in Node.js that Display Hello World ? We will create a simple server in Node.js that returns Hello World using an express server. Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, commonly used to build scalable network applications. One of the fundamental tasks when learning Node.js is creating a simple server that
2 min read
How To Use Node Modules with npm and package.json NodeJS is a powerful runtime for server-side JavaScript & these modules are reusable pieces of code that can be easily imported and used in NodeJS applications. npm (Node Package Manager) is the default package manager for Node JS and is used to install, manage, and publish NodeJS packages. This
3 min read