How to create a web server using Node JS

[Social9_Share type="horizontal"]

How to create a webserver using Node JS

All set. Node is installed. If you have not installed Node please click here to install node .Now, the next step is to understand about each concept of Node and trying some practical programs on the same.In this post, let us learn

Node Core Modules

Node comes with built-in core modules for many types of network and file IO. These include the modules for HTTP, TLS, HTTPS, File system, Datagram and NET.
To include a module within a Node Application, use the require statement. The identifier for the module is passed as a String.


Here ‘http’ is an identifier for the core module HTTP in Node JS.
You can load any core module that is native to Node or modules from node_modules folder, by passing the module identifier within the require function.

Points to be noted

• Module files should can have either .js or .node or .json extension
• Each file has its own module
• The module name is a string of characters or may have forward slashes for path
• Variables are private to the module
• To import a module, use the global ‘require’ function
• To export any function outside the module, use module.exportsEnough of theory. Now let’s jump into coding.

Program to check the OS

This is a simple program to check the os in which the application is running. For this, create a JavaScript file with the below code and save it as ‘one.js’.

Here, we are importing the ‘os’ module. Then, we are printing the os type and its version in which the application is running. Next, to run this file open command prompt, move to the path where ‘one.js’ is present. Then type,

Now, you will see the output in the command line.


Program to create a web server using Node

This program is to show how to create a webserver using Node. First, import the module ‘http’ using require function.

Then, call createServer() method on http which listens to port number 3000.

The process loops behind the scene and continues to do the other operations which are on the way till a request happens. So it will print the message inside console.log() method to command line. Since there is no response to be written, it will print ERR_EMPTY_RESPONSE in the browser

The output in the command line is


Now, let’s add a callback function inside createServer() method. When a request happens, the function(request,response) is fired and prints ‘Welcome to Node’ as response in the browser. This function is triggered, with an onclick event in the browser. The response will be written to the browser, once the response.end() method is called.

To specify the MIME -type of the response in the header, add the below line in the call back function.

The complete program is given below.

The same program can be modified and written as below. Here,the callback function is called on a ‘request’ inside the on method.

The next session discusses all about Node Package Manager.Proceed to NPM – Node Package Manager to learn about this . Till then,
Happy Coding.


  1. Reply
    Sanjana says

    Nice & Clear explanation. Waiting for the next Topic

    • Reply
      admin says

      Thanks Sanjana. Keep visiting.

  2. Reply
    Yasaswini says

    Clear explanation . Waiting for continuation.

Post a comment