App.param() bug?

19 views
Skip to first unread message

sc...@cornell.edu

unread,
Aug 2, 2016, 4:09:25 AM8/2/16
to Express
Hi, all. 

I'm not sure whether this is a bug or intentional, but while this code snippet functions just fine:

app.param(['id', 'page'], function (req, res, next, value) {
  console.log(value);
  next();
});

app.get('/user/:id/:page', function (req, res) {
  console.log('and this matches too');
  res.end();
});


This does not:

app.param(['id-imp', 'page'], function (req, res, next, value) {
  console.log(value);
  next();
});


app.get('/user/:id-imp/:page', function (req, res) {
  console.log('and this matches too');
  res.end();
});

-----
Adding a dash makes the whole thing not function. I understand that there are probably solid reasons for why dashed variable names aren't being accepted, but would it be possible to add a note in the documentation about it? I spent several hours trying to understand why the code wouldn't function and perhaps it can save someone else the time. 

Sincerely, 

Smit Jain. 

hacksparrow

unread,
Aug 25, 2016, 10:58:43 AM8/25/16
to Express
That's the expected behavior. The "-" is interpreted literally, you can't have a param with a "-" in it.

That means, you can do stuff like the following:

app.param(['from', 'to'], function (req, res, next, value) {
  console.log(value);
  next();
});

app.get('/itinerary/:from-:to', function (req, res) {
  console.log(req.params);
  res.end();
});


Reply all
Reply to author
Forward
0 new messages