Js 路由: 可选的 splat param?

我有一条路线是这样的:

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
}
});

然而,这并不工作-如果我调用 path/foo/bar它命中的路线,但如果我调用 path/foo,它不。

是否可以有一个可选的 splat 参数,或者我必须使用正则表达式来检测这一点?

编辑 :

更明确地说,以下是我试图达到的要求:

  • 第一个和第二个参数是 需要
  • 第一个参数是静态的,第二个是命名的参数。
  • 任意数量的 可以选择附加参数可以被追加,并仍然命中路由。
79002 次浏览

Will this do what you're after?

app.all('/path/:namedParam/:optionalParam?',function(req,res,next){
if(!req.params.optionalParam){
// do something when there is no optionalParam
} else {
// do something with optionalParam
}
});

More on Express' routing here, if you haven't looked: http://expressjs.com/guide/routing.html

Here's the current way I'm solving this problem, it doesn't appear that express supports any number of splat params with an optional named param:

app.all(/\/path\/([^\/]+)\/?(.+)?/,function(req,res,next){
// Note: this is all hacked together because express does not appear to support optional splats.
var params = req.params[1] ? [req.params[1]] : [],
name = req.params[0];
if(!params.length){
// do something when there is no splat
} else {
// do something with splat
}
});

I'd love to have this use named params for readability and consistency - if another answer surfaces that allows this I'll accept it.

I just had the same problem and solved it. This is what I used:

 app.get('path/:required/:optional*?', ...)

This should work for path/meow, path/meow/voof, path/meow/voof/moo/etc...

It seems by dropping the / between ? and *, the last / becomes optional too while :optional? remains optional.

The above solutions using optional doesn't work in Express 4. And I tried couple of ways using search patterns, but not working either. And then I found this method and seems fired for unlimited nested path, http://expressjs.com/api.html#router

// this will only be invoked if the path starts with /bar from the mount point
router.use('/bar', function(req, res, next) {
// ... maybe some additional /bar logging ...


// to get the url after bar, you can try
var filepath = req.originalUrl.replace(req.baseUrl, "");


next();
});

It's match all /bar, /bar/z, /bar/a/b/c etc. And after that, you can read req.originalUrl, since params are not filled, ex. you can try compare baseUrl and originalUrl to get the remaining path.

This works for /path and /path/foo on express 4, note the * before ?.

router.get('/path/:id*?', function(req, res, next) {
res.render('page', { title: req.params.id });
});

I got around this problem by using a combination of a middleware that add trailing slashes to url and router.get('/bar/*?', ..., which will pick up everything after /bar/, and return undefined if it is just /bar/. If the visitor asked for /bar, the express-slash middleware will add a slash to the request and turns the request into /bar/.

Suppose you have this url: /api/readFile/c:/a/a.txt

If you want req.params.path to be c::

'/api/readFile/:path*

If you want req.params.path to be c:/a/a.txt:

'/api/readFile/:path([^/]*)'