Skip to main content
node-httpserver-example

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.

Prerequisites

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: https://nodejs.org/.

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 https://nodejs.org 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:

node-http-server>http-server

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

nodejs-http-server

Step 5: Test the Server

Open a web browser or use a tool like cURL to access your server by navigating to http://127.0.0.1:8080/. 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.

OptionDescription
-pPort to use (defaults to 8080)
-aAddress to use (defaults to 0.0.0.0)
-dAddress to use (defaults to 0.0.0.0)
–corsEnable CORS via the Access-Control-Allow-Origin header
-oOpen the browser window after starting the server
-cSet 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/–sslEnable HTTPS, There is an option to set the cert and key file path.
-r/–robotsProvide a /robots.txt
-h/–helpPrint this list and exit

Conclusion

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 *