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
Here is a complete source code for a Node.Js server with above define routes
Handling HTTP requests with different routs
- 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 "/".
- Handling POST request
//repond to a post request on URL"/" app.post('/',function(req,res){ res.send('Posted'); });
- Handling DELETE request
//respond to a delete request on URL "/delete_page" app.delete('/delete_page',function(req,res){ res.send('Deleted'); });
- 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'); });
- 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.
It's always good to skim read through these posts even though I know this, just as a reminder exercise. Nice post - thanks.
ReplyDeleteAgree with you franciskim. Thank you for your comment.cheers!
DeleteGreat article about node.js. I really like your blog very much and also share this article with my friends. Good Job keep it up.
ReplyDeleteSix things that you need to know about MEAN stack
Glad to here that. Thank you Mani OBS.
Delete