首页 > 代码库 > nodejs express route 的用法

nodejs express route 的用法

July 3rd, 2012

nodejs express route 的用法

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

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. 加个路径试试。

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 toolmao

3. 更为复杂一点的,可以把路径作为参数。

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中的代码,也可以写为正则表达式的形式。

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参数中。

一个更复杂的正则的例子,如下:含有2个匹配。

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

请求时,输出如下:

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( ...);

 

nodejs express route
原文地址:http://node-js.diandian.com/post/2012-07-03/40029072624