Aronis Mariano Aronis Mariano - 6 months ago 36
Node.js Question

express.js - single routing handler for multiple routes in a single line

Is there a way to make this on a single function call?

var todo = function (req, res){};

app.get("/", todo);
app.get("/blabla", todo);
app.get("/blablablabla", todo);


Something like:

app.get("/", "/blabla", "/blablablabla", todo );


I know this is a syntax mess, but just for giving an idea of what I would like to achieve, an array of the routes would be awesome!

Anyone know how to do this?

Answer
app.get('/:var(bla|blabla)?', todo)

:var sets the req.param that you don't use. it's only used in this case to set the regex.

(bla|blabla) sets the regex to match, so it matches the strings bla and blablah.

? makes the entire regex optional, so it matches / as well.