Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Which component can be used to route multiple data over a single line?

Multiplexer means many into one. A multiplexer is a circuit used to select and route any one of the several input signals to a single output. A simple example of an non-electronic circuit of a multiplexer is a single pole multi-position switch.

How do I Group A route in node JS?

You can use an npm moduleYou can group your middlewares as an array and pass it to the express-inject-middleware... Show activity on this post. in express 4 to grouping your routes, you should create some changes : seperate route files in multiple files like admin and front.

What is route handler in Express JS?

As a generic term, a route handler is code that is looking for a request to a specific incoming URL such as /login and often a specific HTTP verb such as POST and has specific code for handling that precise URL and verb. Some examples: Serve a specific web page.

What is routing and how routing works in Express JS?

Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app. get() to handle GET requests and app.


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.


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.


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);

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();
});