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 Simple Web Server with Golang? Golang is a procedural programming language ideal to build simple, reliable, and efficient software. Creating Web Servers Using Golang: Initialize a projectCreate a project folder with a .go file inside eg. server.go. Directory structure:The server.go file:Gopackage main import ( "fmt" "log" "net/ht
2 min read
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 use Routes with serve-static Files in Node.js ? Using serve-static files with routes in a Node.js application involves creating a server with Express, defining routes, and serving static files like HTML, CSS, JavaScript, images, etc. Hereâs an article explaining how to use serve-static with routes in Node.js.Serving Static Files with Routes in No
3 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 use SSL/TLS with Node.js ? TLS/SSL is used for establishing secure connections over the internet. Today, most websites use HTTPS to communicate with clients. HTTPS is basically HTTP running over TLS/SSL. Web clients like browsers alert users about websites that do not use HTTPS since such websites are vulnerable to cyber-atta
5 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 TypeScript to build Node.js API with Express ? TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will
4 min read
How to use OpenCV with Node.js? OpenCV is a free-to-use cross-platform programming library that is mainly utilized for computer vision. Â It is written in C/C++. Computer vision is widely used in applications like face detection, autonomous vehicles, etc. To use this with Node.js we will use opencv4nodejs library. Opencv4nodejs all
3 min read
Node.js http.ServerResponse.socket Api The httpServerResponse.socket is an inbuilt application programming interface of class ServerResponse within http module which is used to get the reference of the underlying socket object. Syntax: response.socket Parameters: This method does not accept any arguments as a parameter. Return Value: Thi
2 min read