14

The following:

app.get('/foo/start/:start/end/:end', blah.someFunc);

matches

/foo/start/1/end/4

but I want it to also match an optional parameter

/foo/start/1/end/4/optional/7

I've tried this:

app.get('/foo/start/:start/end/:end(/optional/:value)?', blah.someFunc);

but it doesn't match either of the above two examples. I think it's because I'm trying to give it a RegExp when it's expecting something else?

Thanks.

1
  • You cannot mix paths with RegExps which is what you are doing. Use either a path or a RegExp only. expressjs.com/api.html#app.VERB
    – tim
    Commented Sep 4, 2013 at 16:30

4 Answers 4

22

Why don't you add another rule before the one you have, like this

app.get('/foo/start/:start/end/:end/optional/:value', blah.someFunc);
app.get('/foo/start/:start/end/:end', blah.someFunc);

It will be used before the one without the optional value.

If you want to use just one line try this:

app.get('/foo/start/:start/end/:end/optional?', blah.someFunc)

see the docs for an example.

3
  • 2
    Yeh, I can do but I thought it would be cleaner to have one rule (if possible) :)
    – ale
    Commented Sep 4, 2013 at 16:24
  • 2
    How much more? Do you have some data I can look at about this? Commented May 2, 2014 at 19:20
  • 1
    The hidden maintenance issue here is that ordering is important. If some misinformed dev comes along later and switches the ordering of your route definitions, your functionality just disappears. You can handle this with one route using regular expressions, which is a standard feature of express.Router: expressjs.com/4x/api.html#app.VERB Commented Oct 13, 2014 at 17:19
19

If you are using Express 4.x Then I think its better to use array format for route. For example I have route /service which gives all service list and same route when used with id /service/id/:id gives single service with id in the param.

app.get(['/service', '/service/id/:id'], function(req, res) {});
2
  • Clean answer, only question is can I avoid writing '/service' twice?
    – orepor
    Commented May 9, 2017 at 6:35
  • that need regular expression , this is clean.
    – Arun Killu
    Commented May 12, 2017 at 11:34
6

In this example, if the url is /hello or /hello/world it works. The ? makes the parameter become optional (express 4).

// app.js
var index = require('/routes/index');
app.use('/hello', index);

// routes/index.js
router.get('hello/:name?', function(req, res){
  var name = req.params.name;
  var data = {
    name: name
  };
  res.json(data);
});
5

You can also use regular expressions in routes. Perhaps something like:

app.get(/^\/foo\/start\/:start\/end\/:end(\/optional\/:value)?/, function (req, res, next) {
3
  • 4
    This looks dirty dude :) Commented Oct 13, 2014 at 13:01
  • 10
    Why? Express translates all route strings to regular expressions. Using regexes directly is referred to all over the documentation: expressjs.com/4x/api.html#request Commented Oct 13, 2014 at 17:18
  • 1
    I know that, it was sarcasm. Commented Oct 13, 2014 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.