Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to create an http server in Node.js

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 108
    Comment on it

    This tutorial will help a user to create an http server in Node.js and run an applications at server side.

    Follow the steps as below:

    1. Install Node.js
    2. Lets create a file test.js which we will place in the root directory of our application

    To create an http server we will use a module of Node named server in our file test.js

    // the builtin function require helps to use any module of Node.js   
    var testhttp = require("http"); 
    // In the below line createServer is a function in http module of Node.js    
    var server = testhttp.createServer(on_Request);
    function on_Request(request, response) {
    // In the below line writeHead is a function of response object
       response.writeHead(200, {"Content-Type": "text/plain"});
    // In the below line write is a function of response object
    response.write("Yes my testhttp server is working..");
    //end function of response object ends the response
    // listen is a function of the object that is returned by the function createServer
    // the listen function needs one paramater that represent the port number on which the testhttp server //will listen the reqeusts.

    3-Now go to the terminal and execute the following command

    user@username:~$ cd /var/www/html/project_root_directory
    user@username:/var/www/html/project_root_directory$ nodejs test.js

    4- Now open a browser and write the following url:


    output on the browser: Yes my testhttp server is working..
    output on the terminal:  testhttp server has started

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: