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

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 3.35k
    Comment on it

    This tutorial will help user to learn how to use request handler to handle different URLs. For this we will create another module named requestHandler wherein we can define some functions and the request to these functions will be handled by the reqeust handlers.


    1- First we will create requestHandler.js wherein we will define some functions and export them.

    function find() {
            console.log("Request handler 'find' was called.");
        }
    
    
        function display() {
            console.log("Request handler 'display' was called.");
        }
    
    exports.find      = find;
    exports.display = display;
    

    2- Now we will create an index.js file or our main file wherein we will pass list of
    requestHandler object.We will also use our new module requestHandler using builtin function require of Node.js

    var testhttp_obj = require("./test");
    var router = require("./router");
    var requestHandler = require("./requestHandler");
    
    var handle = {};
    
    handle['/']         = requestHandler.find;
    handle['/find']     = requestHandler.find;
    handle['/display']= requestHandler.display;
    
    testhttp_obj.execute_obj(router.route_request_obj,handle);
    

    3- Here we will create test.js file for creating an http server using http module of Node.js Here we will pass the parameters to execute function.

    var testhttp = require("http");
    var url  = require("url"); 
    
    function execute(route_request_obj,handle){
    
        function onRequest(request, response) {
            var path = url.parse(request.url).pathname; 
            route_request_obj(handle,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);
        console.log("testhttp server has started.");
    }
    
    exports.execute_obj = execute;
    

    4- Now create a router.js file and pass the parameter handle and path in the route_request
    function.

    function route_request(handle,path){
    
        if(typeof handle[path] === 'function'){
                handle[path]();
        } else if(path === '/favicon.ico'){
            //keep this check so that the message do not appear as we always get the default path           //request for /favicon.ico. 
        } else{
            console.log("No request handler found for path: " + handle);
        }
    }
    
    exports.route_request_obj = route_request;
    

    5- 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 terminal:  testhttp server has started
    
    Hit the url on the browser :http:localhost:8831/index  
    
            On the browser we get : Request for /index received 
            On the terminal we get: No request handler found for path: [object Object]
    
    Hit the url on the browser :http:localhost:8831/find 
    
            On the browser we get : Request for /find  received 
            On the terminal we get: Request handler 'find' was called.
    

 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: