Node.Js : Basic routing with Express

Express framework provide different basic ways to configure routing in our web application. Check out my previous post of how to set up a simple HTTP server with Express and Node.Js if you are new to Node and Express.

Handling HTTP requests with different routs  
  1. Handling GET request
    
    //respond to a get request on URL "/"
    app.get('/',function(req,res){
     res.send('Hello Express');
    });
    

    Here URL can be specified as we want instead of "/". 
  2. Handling POST request
    
    //repond to a post request on URL"/"
    app.post('/',function(req,res){
     res.send('Posted');
    });
    


  3. Handling DELETE request
    
    //respond to a delete request on URL "/delete_page"
    app.delete('/delete_page',function(req,res){
     res.send('Deleted');
    });
    


  4. Handling URLs with wild cards
    
    //respond to a get request on URL "/index", "/inbox", "/in123x" os on.
    app.get('/in*x',function(req,res){
        var pathname = url.parse(req.url).path;
        
     res.send('You visited to - '+pathname);
    });
    //respond to get request on any URL except the ones defined earlier
    app.get('*',function(req,res){
     res.send('bad rout');
    });
    


  5. Handling parameters with URL
    
    //respond to a get request with extra parameters
    app.get('/who/:name?',function(req,res){
    var name=req.params.name;
     res.send('This page about '+name);
    });
    
    app.get('/who/:name?/:title?',function(req,res){
    var name=req.params.name;
    var title=req.params.title;
     res.send('This page about '+name+' title:'+title);
    });

Here is a complete source code for a Node.Js server with above define routes

var express=require('express');
var url = require("url");

var app=express();

//respond to a get request on URL "/"
app.get('/',function(req,res){
 res.send('Hello Express');
});

//repond to a post request on URL"/"
app.post('/',function(req,res){
 res.send('Posted');
});

//respond to a delete request on URL "/delete_page"
app.delete('/delete_page',function(req,res){
 res.send('Deleted');
});

//respond to a get request on URL "/index", "/inbox", "/in123x" os on.
app.get('/in*x',function(req,res){
    var pathname = url.parse(req.url).path;
    
 res.send('You visited to - '+pathname);
});

//respond to a get request with extra parameters
app.get('/who/:name?',function(req,res){
var name=req.params.name;
 res.send('This page about '+name);
});

app.get('/who/:name?/:title?',function(req,res){
var name=req.params.name;
var title=req.params.title;
 res.send('This page about '+name+' title:'+title);
});

app.get('*',function(req,res){
 res.send('bad rout');
});


var server=app.listen(3000,function(){
 console.log('listening');
});

Those basic routing can be used in your node.js application creatively to achieve what you need.

4 comments:

  1. It's always good to skim read through these posts even though I know this, just as a reminder exercise. Nice post - thanks.

    ReplyDelete
    Replies
    1. Agree with you franciskim. Thank you for your comment.cheers!

      Delete
  2. Great article about node.js. I really like your blog very much and also share this article with my friends. Good Job keep it up.

    Six things that you need to know about MEAN stack

    ReplyDelete