express.js - single routing handler for multiple routes in a single line
Solution 1
I came across this question while looking for the same functionality.
@Jonathan Ong mentioned in a comment above that using arrays for paths is deprecated but it is explicitly described in Express 4, and it works in Express 3.x. Here's an example of something to try:
app.get(
['/test', '/alternative', '/barcus*', '/farcus/:farcus/', '/hoop(|la|lapoo|lul)/poo'],
function ( request, response ) {
}
);
From inside the request
object, with a path of /hooplul/poo?bandle=froo&bandle=pee&bof=blarg
:
"route": {
"keys": [
{
"optional": false,
"name": "farcus"
}
],
"callbacks": [
null
],
"params": [
null,
null,
"lul"
],
"regexp": {},
"path": [
"/test",
"/alternative",
"/barcus*",
"/farcus/:farcus/",
"/hoop(|la|lapoo|lul)/poo"
],
"method": "get"
},
Note what happens with params: It is aware of the capture groups and params in all of the possible paths, whether or not they are used in the current request.
So stacking multiple paths via an array can be done easily, but the side-effects are possibly unpredictable if you're hoping to pick up anything useful from the path that was used by way of params or capture groups. It's probably more useful for redundancy/aliasing, in which case it'll work very well.
Edit: Please also see @c24w's answer below.
Edit 2: This is a moderately popular answer. Please keep in mind that ExpressJS, as with most Node.js libraries, is a moveable feast. While the routing above does still work (I'm using it at the moment, a very handy feature), I cannot vouch for the output of the request object (it's certainly different from what I've described). Please test carefully to ensure you get the desired results.
Solution 2
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.
Solution 3
You can actually pass in an array of paths, just like you mentioned, and it works great:
var a = ['/', '/blabla', '/blablablabla'];
app.get(a, todo);
Solution 4
Just to elaborate on Kevin's answer, this is from the 4.x docs:
The path for which the middleware function is invoked; can be any of:
- A string representing a path.
- A path pattern.
- A regular expression pattern to match paths.
- An array of combinations of any of the above.
They have some examples, including:
This will match paths starting with
/abcd
,/xyza
,/lmn
, and/pqr
:app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) { next(); });
Solution 5
I went for a:
['path', 'altPath'].forEach(function(path) {
app.get(path, function(req, res) { etc. });
});
Aronis Mariano
Senior Backend developer, Data scientist, Math lover, Open Source enthusiast.
Updated on May 17, 2020Comments
-
Aronis Mariano almost 4 years
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?
-
brandonscript about 10 yearsWhat's the replacement for this being deprecated?
-
joscarsson over 9 years
/bla(bla)?
also works, but any parameters after does not populate properly (ie/bla(bla)?/:value
does not populatereq.params.value
). Anyone know why? -
Matt The Ninja over 8 yearsIf your using express you can get it with
req.params.var
but you need to make sure you havereq
passed to the function -
nbro over 8 years@JonathanOng You say this is deprecated, and remus is asking what's the replacement. Could you please answer his/her question?
-
danday74 over 7 yearsrouter.route(['/mailer/out/system', '/mailer/out/payment']) is working just fine in express 4 no deprecation warning
-
Jason Axelson about 7 years@JonathanOng looking at the current Express v4 docs using an array does not appear to be deprecated.
-
SkyDev about 2 yearswhat if I would like to apply middleware for different path and methods. router.get('/:id', validate.itemExists('unit_status'), unitStatusControllers.findOne); router.put('/:id', [validate.checkPermission('edit:unitstatuses'), validate.itemExists('unit_status')], unitStatusControllers.update); router.delete('/:id', [validate.checkPermission('delete:unitstatuses'), validate.itemExists('unit_status')], unitStatusControllers.remove); As you see here I am calling individual itemExists middleware for Get, Put, Delete. Your solution doesn't include for different methods. Any opinion?
-
Kevin Teljeur about 2 yearsThis probably should be a separate question on StackOverflow. You will need different routings for different methods, my solution is just for highlighting what you can do to stack routings on a single method where you want the same function to be called (including middleware). Middleware is an additional complexity, although there's no reason why you couldn't use the route I've described for middleware, no matter which method.