Skip to main content

Creating a Simple HTTP Server in Node.js with Examples

In this article, we’ll walk through the process of creating a simple HTTP server using http-server Node.js with examples.

The http-server is used for your static file-serving, it’s a simple and effective way to serve static files during development or for web applications.

Whats Nodejs

Node.js is a powerful open-source runtime environment for JavaScript, that operates seamlessly across different platforms. It leverages the V8 engine from Chrome to execute JavaScript code independently of web browsers.

Whats HTTP Server

Node http-server is a simple and zero-configuration command-line HTTP server, You can use it for production usage but as per official docs it’s hackable enough to be used for testing, local development and learning.

  • HTTP: This is a built-in HTTP server module in nodejs, You have seen many nodejs application has included using require(‘http‘).
  • HTTP Server: This is another module that helps to create a web server in your system that will act as a web server like another apache, nginx etc.

Why do we need HTTP Server

This is very simple and easy to use, The only dependency is nodejs and nothing else, nowadays we have a front-end framework that is running an HTTP server, like angular, backbone js etc. You can install Apache or Nginx but node http-server is an awesome module to create a web server in a local or prod environment.


Please make sure you have Node.js and NPM are installed on your computer. If you haven’t already, You can download and install it from the official Node.js website:

Also Checkout other tutorials of nodejs,

Building a Basic HTTP Server

Let’s start to create a Node simple HTTP server. We’ll create a folder and add a file to implement this.

Step 1: Set up Your Project

Create a new directory node-http-server in your project and navigate to it in your terminal. You can use the following commands:

mkdir node-http-server
cd node-http-server

Step 2: How to Install HTTP Package

Node uses npm to install any modules, so we will use npm to install http-server with -g option.
npm install http-server -g

-g option will install http-server globally so that you can run the HTTP server from the command line, Now you can visit http://localhost:8080 to view your server.

if you are getting an error in the cmd window, that means you haven’t installed nodejs yet in your system, you can download the latest stable release of NodeJS from and install it using all the default options.

Step 3: Create a JavaScript File

Inside your node-http-server directory, create a JavaScript file (e.g., server.js) where you’ll write your Node.js server code.

Step 4: Run the Server

Save the server.js file and run the server by executing the following command in your terminal within the project directory:


You should see the message “Server running at” displayed in your terminal, indicating that the server is up and running.


Step 5: Test the Server

Open a web browser or use a tool like cURL to access your server by navigating to You should see the server starting message displayed in the command window

Serve HTML Content Using Inbuilt HTTP Server

You can also serve HTML pages, static files (e.g., CSS, JavaScript, images), or dynamically generate content based on the request. You need to set the Content-Type header to 'text/html' to server HTML content.

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

HTTP Server Option

There are a lot of options available to configure http-server but here I will list down some daily usage http-server options.

Option Description
-p Port to use (defaults to 8080)
-a Address to use (defaults to
-d Address to use (defaults to
–cors Enable CORS via the Access-Control-Allow-Origin header
-o Open the browser window after starting the server
-c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to ‘3600’). To disable caching, use -c-1
-S/–ssl Enable HTTPS, There is an option to set the cert and key file path.
-r/–robots Provide a /robots.txt
-h/–help Print this list and exit


This tutorial helps to create a node js simple web server, There are two modules available in Node js that allow Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).

Leave a Reply

Your email address will not be published. Required fields are marked *