nodejs express route 的用法

From: http://node-js.diandian.com/post/2012-07-03/40029072624


nodejs express route 的用法

1. 首先是最基本的用法。node

1
2
3
4
5
var app=require( 'express' ).createServer();                                                    
app.get( "/" , function (req,res){                                                    
     res.send( "hello world" );                                                    
});                                                    
app.listen(3000);

當用戶訪問 127.0.0.1:3000的時候,頁面會輸出hello world正則表達式

2. 加個路徑試試。express

1
2
3
4
5
6
7
var app=require( "express" ).createServer();                                               
                                                                                                  
app.get( "/toolmao" , function (req,res){                                               
     res.send( "welcome to toolmao" );                                               
});                                               
                                                                                                  
app.listen(3000);

當用戶訪問 127.0.0.1:3000/toolmao的時候,就會輸出welcome to toolmaoapp

3. 更爲複雜一點的,能夠把路徑做爲參數。curl

1
2
3
4
5
6
7
var app=require( "express" ).createServer();                                           
                                                                                          
app.get( '/user/:id' , function (req, res){                                           
     res.send( 'user ' + req.params.id);                                           
});                                           
                                                                                          
app.listen(3000);

當用戶訪問 127.0.0.1:3000/user/gainover 的時候,就會輸出 user gainover函數

4. 3中的代碼,也能夠寫爲正則表達式的形式。post

1
2
3
4
5
6
7
var app=require( "express" ).createServer();                                       
                                                                                  
app.get(/\/user\/([^\/]+)\/?/, function (req, res){                                       
     res.send(req.params);                                       
});                                       
                                                                                  
app.listen(3000);

這裏能夠根據你的須要進行正則的自定義。正則中的匹配結果,存儲在req.params參數中。fetch

一個更復雜的正則的例子,以下:含有2個匹配。ui

1
2
3
app.get(/^\/users?(?:\/(\d+)(?:\.\.(\d+))?)?/, function (req, res){                                  
     res.send(req.params);                                  
});

請求時,輸出以下:url

1
2
3
4
5
6
7
8
$ curl http: //dev:3000/user                                
[ null , null ]                                
$ curl http: //dev:3000/users                                
[ null , null ]                                
$ curl http: //dev:3000/users/1                                
[ "1" , null ]                                
$ curl http: //dev:3000/users/1..15                                
[ "1" , "15" ]

5. 若是咱們想指定參數爲id,同時又想用正則進行限制,能夠寫爲:

/user/:id([0-9]+)

----------------------------------------------------------------------------

Route的依次執行

1. 當一個請求,可以匹配到多個route時,咱們能夠調用內置的next函數,來依次進行處理。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
app.get( '/users/:id?' , function (req, res, next){                       
     var id = req.params.id;                       
     if (id) {                       
         // do something                       
     } else {                       
         next();                       
     }                       
});                       
                                                  
app.get( '/users' , function (req, res){                       
     // do something else                       
});

當用戶請求,/users/gainover時,能夠進行某種處理,而當用戶請求爲/users/, id 不存在,則會調用next()函數,進而調用 app.get("/users/", ....);

2. 一個route裏能夠有多個處理函數。例如:

app.get('/users/:id/edit/',function1,function2,...);

一個實際的例子可能以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function loadUser(req, res, next) {           
   // You would fetch your user from the db           
   var user = users[req.params.id];           
   if (user) {           
     req.user = user;           
     next();           
   } else {           
     next( new Error( 'Failed to load user ' + req.params.id));           
   }           
}           
function andRestrictToSelf(req, res, next) {           
   req.authenticatedUser.id == req.user.id           
     ? next()           
     : next( new Error( 'Unauthorized' ));           
}           
                          
app.get( '/user/:id/edit' , loadUser, andRestrictToSelf, function (req, res){           
   res.send( 'Editing user ' + req.user.name);           
});

當用戶訪問:/user/gainover/edit時,首先會調用第一個處理函數loadUser,判斷用戶是否存在於users中,若是不存在,經過next(new Error(msg)); 的方式拋出異常,不然,執行next(),而next此時實際就是指向 andRestrictToSelf 函數,而後判斷當前登陸的id和被編輯的id是否等同,若是等同,則繼續next(),從而執行 res.send( ...);

相關文章
相關標籤/搜索