I want my express app to match requests with both file/a and file/a/b/c, the number of variables after the file/ is at least 1.

I tried this:

app.get('/file/:path?*', function(req,res){
    res.send(req.params.path);   
});

However the result is:

$ curl localhost:3000/file/a
a
$ curl localhost:3000/file/a/b
a

How can I get the whole query by not using req.query.splice(1) ? I want the code to be more readable, this splice can be easily forgotten during maintenance.

有帮助吗?

解决方案

Have a look at their documentation, there is a line about it:

Regular expressions may also be used, and can be useful if you have very specific restraints, for example the following would match "GET /commits/71dbb9c" as well as "GET /commits/71dbb9c..4c084f9".

app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){
  var from = req.params[0];
  var to = req.params[1] || 'HEAD';
  res.send('commit range ' + from + '..' + to);
});

This should solve your problem :)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top