glog glog - 21 days ago 6
Node.js Question

Express router - :id?

Real simple question guys: I see a lot of books/code snippets use the following syntax in the router:

app.use('/todos/:id', function (req, res, next) {
console.log('Request Type:', req.method);
next();
});


I'm not sure how to interpret the route here... will it route '/todos/anything'? and then grab the 'anything' and treat is at variable ID? how do I use that variable? I'm sure this is a quick answer, I just haven't seen this syntax before.

Answer Source

This is an express middleware.

In this case, yes, it will route /todos/anything, and then req.params.id will be set to 'anything'