How do I specify a route with a parameter that can be empty?
e.g. /:one?/:two?
handles /1/2
, but does not handle //2
, how do I a make a route to catch both the uris?
Join Stack Overflow to learn, share knowledge, and build your career.
app.get(/\/(.*)\/(.*)/, function(req, res) {
var one = req.params[0];
var two = req.params[1];
});
/a/b => {0:'a', 1:'b'}
/a/ => {0:'a', 1:'' }
//b => {0:'' , 1:'b'}
// => {0:'' , 1:'' }
req.params.one/two
? I'm afraid not. When you use regex you can't use named parameters.
– laggingreflex
Feb 9 '15 at 16:19
//
,/1/
, and/1/2
should all work – Fluffy Feb 9 '15 at 15:00one
is empty the request becomes/2
, not//2
, and what you have done will serve/2
ifone
is empty. – Naeem Shaikh Feb 9 '15 at 15:00