Express.js - the only routing handler for multiple routes on one line

Is there a way to do 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 syntactical mess, but just to give an idea of ​​what I would like to achieve, the array of routes would be awesome!

Does anyone know how to do this?

+61
express
Mar 11 '13 at 22:39
source share
6 answers

I came across this question looking for the same functionality.

@Jonathan Ong mentions 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 is 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 the path /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 to the parameters: he knows the capture groups and parameters in all possible ways, regardless of whether they are used or not in the current request.

Thus, stacking multiple paths through an array can be done easily, but the side effects are probably unpredictable if you are hoping to get something useful from a path that was used as parameters or capture groups. This is probably more useful for redundancy / smoothing, in which case it will work very well.

Edit: see also @ c24w answer below .

Edit 2: This is a moderately popular answer. Keep in mind that ExpressJS, like most Node.js libraries, is a moving holiday. Although the above routing still works (I am using it at the moment, it’s a very convenient function), I can’t vouch for the output of the request object (this, of course, is different from what I described). Please test thoroughly to make sure you get the results you want.

+42
Sep 26 '14 at 11:18
source share
 app.get('/:var(bla|blabla)?', todo) 

:var installs req.param , which you are not using. it is used only in this case to set the regular expression.

(bla|blabla) matches the regular expression, so it matches the bla and blablah .

? makes the entire regular expression optional, so it also matches / .

+47
Mar 11 '13 at
source share

In fact, you can pass an array of paths as you mentioned, and it works just fine:

 var a = ['/', '/blabla', '/blablablabla']; app.get(a, todo); 
+42
Jun 17 '13 at 19:52
source share

To describe Kevin's answer in detail, this is from 4.x docs :

The path for which the middleware function is called; can be any of:

  • A string representing the path.
  • Path template.
  • A regular expression pattern for matching paths.
  • An array of combinations of any of the above.



They have examples , including:

This will match the paths starting with /abcd , /xyza , /lmn and /pqr :

 app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) { next(); }); 
+13
Jun 02 '16 at 16:01
source share

I went for:

 ['path', 'altPath'].forEach(function(path) { app.get(path, function(req, res) { etc. }); }); 
+4
Nov 30 '16 at 10:18
source share

requires a source route file and define a new route like this

 var user = require('./users'); router.post('/login', user.post('/login')); 
0
Aug 30 '16 at 15:59
source share



All Articles