SHARE
Facebook X Pinterest WhatsApp

Node.js Web Server: How to Create an HTTP Server

Sep 15, 2017

Node.js is a lightweight, open source, event-driven, cross-platform environment built on Google Chrome’s JavaScript V8 Engine. It is used for building server-side applications with one of the most popular scripting languages invented to date, JavaScript. In this article, we will examine how we can implement a minimalistic web server using Node.js.

Why should I use it?

So, when should I use Node.js? Well, Node.js is a good choice in all of the following scenarios.

  • I/O applications
  • Applications that are data intensive
  • Applications that require a lot of data streaming

However, you should not use Node.js for applications that are CPU-intensive. In other words, applications that are I/O intensive can benefit from Node.js but not applications that are CPU intensive.

Features of the Node.js runtime

Let’s take a quick look at the salient features of Node.js.

  • Event-driven: Node.js is a non-blocking, event-driven runtime. In essence, a Node.js server would not wait for the response from the previous call. Rather, it would move on to the next call. Node.js takes advantage of events to get notifications on the response of a call.
  • Performance: Node.js is asynchronous and fast. It doesn’t use buffering and transfers data in chunks instead. Most importantly, I/O in Node.js is asynchronous (i.e., non-blocking) and event driven.
  • Scalable: Node.js is single-threaded with event looping and is a highly scalable environment. Unlike many traditional servers, Node.js uses a single thread and works in a non-blocking way — using an event looping mechanism.

Getting started

Download a copy of using Node.js. While installing, ensure that you specify the default options.

Building a minimalistic web server

And, now, let’s take advantage of Node.js to build a simple web server. Incidentally, a web server is an application that can receive HTTP requests and return HTTP responses.

If Node.js is already downloaded and installed in your system, you can easily set up a simple HTTP web server on your system using Node.js. To do this, follow these steps outlined below:

  1. Open a Command Prompt Window in the administrator mode
  2. Type the following command there:
    npm install -g http-server
  3. Switch to the directory that contains the static website files
  4. Next, start the web server using the following command
    http-server

That’s all you need to do. A minimalistic web server has been setup locally in your system. You can now browse http://localhost:8080 from your web browser.

Alternately, you can also write your own web server using Node.js using code. Here’s the code listing that you can use to achieve this.

var http=require('http')
var server=http.createServer((function(request,response)
{
	response.writeHead(200,
	{"Content-Type" : "text/plain"));
	response.end("This is a minimalistic web server using Node.js");
}));
server.listen(8080); 

Assuming that the above script has been stored in a file named MyNodeServer.js, you can use the following command to start your simple web server from the command prompt.

node MyNodeServer.js

Summary

Node.js has gained a lot of popularity due to its ability to write server-side applications using JavaScript, along with its simplicity, ease of use, performance and scalability. This article presented an overview of Node.js and discussed how to build a simple web server using Node.js. I will discuss more features of Node.js in my future posts.

Recommended for you...

Best VR Game Development Platforms
Enrique Corrales
Jul 21, 2022
Best Online Courses to Learn HTML
Ronnie Payne
Jul 7, 2022
Working with HTML Images
Octavia Anghel
Jun 30, 2022
Web 3.0 and the Future Of Web Development
Rob Gravelle
Jun 23, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.