Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to route requests in Node.js

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 129
    Comment on it

    This tutorial will help, learn how to route the requests in Node.js, for correct routing it is necessary to get the exact pathname and pass correct values via query string. We can get all these information through the request object. Hence to get right pathname and parameters that are passed via querystring we use url and querystring modules of Node.js URL module helps to extracts requested path and the querystring whereas querystring module parse the query string and get the requested parameters.


    1- Now we will create a test.js file with the following code:

    var testhttp = require("http");
    var url  = require("url"); 
    
    function execute(route_request_obj){
    
        function onRequest(request, response) {
    
        var path = url.parse(request.url).pathname; 
    
        route_request_obj(path);
    
        // 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 this will display on the browser
        response.write('Request for '+path+ '  received');
    
        //end function of response object ends the response
        response.end();
    
    }
    
    // In the below line createServer is a function in http module of Node
    var server = testhttp.createServer(onRequest);
    server.listen(8831);
    }
    
    exports.execute_obj = execute;
    console.log("testhttp server has started.");
    

    2- Now create another file router.js

      function route_request(path){
        console.log("About to route a request for " +path); 
    }
    
    exports.route_request_obj = route_request;
    

    3- Create a file index.js

    var testhttp_obj = require("./test");
    var router = require("./router");
    
    testhttp_obj.execute_obj(router.route_request_obj);
    

    4- 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 index.js
    
    
    output on the browser: Request for /index.js  received
    output on the terminal:  testhttp server has started
                              about to route a request for  /index.js
                              about to route a request for  /favicon.ico  //put a check to avoid the message for favicon.ico
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: