Paraknight Paraknight - 4 months ago 5
Node.js Question

How can I create an Express route that handles URLs that look like this?

app.get('/:service[SOMETHING GOES HERE]', function(req, res, next){
console.log('Service is:', req.params.service);

This needs to catch URLs that can look like any one of:


The call back isn't concerned with anything that comes after foo, but ideally should be able to access foo as a parameter called
without having to manually parse the path.

I've been using this to test and still haven't found anything that does exactly that. Closest so far is

Edit: No it's not a duplicate of the one where the answer is
because that doesn't cover


Using /:service* in actual Express routes does exactly what you want:

/foo           maps to { '0': '', service: 'foo' }
/foo/bar       maps to { '0': '/bar', service: 'foo' }
/foo/bar/blah  maps to { '0': '/bar/blah', service: 'foo' }

The Express Route Tester, for some reason, maps these URL's differently for that these kinds of patterns (it might be configured differently than Express).