Implementing an HTTP Web Server in Node.js

Version 1

    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:
    Open a Command Prompt Window in the administrator mode
    Type the following command there:
    npm install -g http-server
    Switch to the directory that contains the static website files
    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:8080from 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
    Read original article here