我有一条看起来像这样的路线:
app.all('/path/:namedParam/*splat?',function(req,res,next){
if(!req.params.length){
// do something when there is no splat
} else {
// do something with splat
}
});
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用 - 如果我打电话给path/foo/bar它击中路线,但如果我打电话path/foo,它不会.
是否可以使用可选的splat参数,或者我是否必须使用正则表达式来检测它?
编辑:
为了更清楚,这是我想要实现的要求:
我在person集合中的MongoDB密钥是这样的:
TWITTER/12345678
GOOGLE/34567890
TWITTER/45678901
...
Run Code Online (Sandbox Code Playgroud)
我用getPersonByKey这种方式定义路线:
router.route('/getPersonByKey/:providerKey/:personKey').
get(function(req, res) { // get person by key
var key = req.params.providerKey + '/' + req.params.personKey;
// ...
}
);
Run Code Online (Sandbox Code Playgroud)
当然我更愿意写这样的东西:
router.route('/getPersonByKey/:key').
get(function(req, res) { // get person by key
var key = req.params.key;
// ...
}
);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为GET http://localhost/getPersonByKey/TWITTER/12345678当然会产生404,因为带斜杠的参数被解释为两个不同的参数......任何想法?