每種方式都有不同的效果
下面會示範各種寫法
1.
在下面的程式
app.get('/', routes.index);
app.get('/users', user.list);
後面加上//It can cover /route/
app.get('/route', function(req, res, next) {
res.send('This path has no / (/route)');
});
//It is no use
app.get('/route/', function(req, res, next) {
res.send('This path has / (/route/)')
});
//It is use.
app.get('/route2/', function(req, res, next) {
res.send('This path has / (/route2/)')
});
app.get('/route2', function(req, res, next) {
res.send('This path has no / (/route2)');
});
執行後會發現在app.get程式的順序會影響判斷的順序
只要判斷成功就會直接結束
所以上面的可以發現 /route/ 可以被判斷為 /route
但是 /route 不能被判斷為 /route/
2.
加入param的值
一樣在app.get之後加上
//It can't cover /list
app.get('/list/:num', function(req, res, next) {
var num = req.params.num;
res.send('This is test num:' + num);
});
//It can't cover /list2/:num
app.get('/list2/:num/:num2', function(req, res, next) {
var num = req.params.num;
var num2 = req.param('num2');
var string = '';
string += 'This is test num:' + num + '<br />';
string += 'This is test num2:' + num2 + '<br />';
res.send(string);
});
執行後會發現在param也是同樣情形
只會進入正確的地方









0 意見:
張貼留言