Express.js routing: optional slpat parameter?

I have a route that looks like this:

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

however this does not work - if I call path/foo/bar , it gets into the route, but if I call path/foo , it is not.

Is it possible to have an optional splat parameter, or do I need to use a regular expression to detect it?

Edit

to be more clear, here are the requirements I'm trying to achieve:

  • first and second parameters required
  • the first parameter is static, the second is a named parameter.
  • any number of optional additional parameters can be added and still gets into the route.
+74
express
Apr 04 '12 at 22:21
source share
6 answers

This works for / path and / path / foo on express 4, pay attention to * before ? .

 router.get('/path/:id*?', function(req, res, next) { res.render('page', { title: req.params.id }); }); 
+95
Apr 09 '15 at 21:13
source share

I had the same problem and it was resolved. This is what I used:

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

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

Seems to drop / between ? and * , the last / becomes optional, and :optional? remains optional.

+67
Jan 23 '13 at
source share

Will this do what you need?

 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

+14
Apr 04 '12 at 22:30
source share

In this method, I solve this problem, it does not seem that express supports any number of splat parameters with the optional name 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 would like this use to be called params for readability and consistency - if other response surfaces that allow this, I will accept it.

+3
Apr 05 2018-12-12T00:
source share

The above solutions using optional do not work in Express 4. And I tried to use several methods using search templates, but not working. And then I found this method and seem to be fired for an 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 matches all / bar, / bar / z, / bar / a / b / c, etc. After that you can read req.originalUrl, since params are not populated, for example. you can try comparing baseUrl and originalUrl to get the remaining path.

0
Mar 17 '15 at 18:14
source share

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

0
Jun 15 '18 at 4:48
source share



All Articles