Tuesday, March 19, 2024

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

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.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured