Express-js wildcard routing to cover everything under and including a path

前端 未结 5 1234
暖寄归人
暖寄归人 2020-12-02 16:34

I\'m trying to have one route cover everything under /foo including /foo itself. I\'ve tried using /foo* which work for everything

相关标签:
5条回答
  • 2020-12-02 16:50

    It is not necessary to have two routes.

    Simply add (/*)? at the end of your path string.

    For example, app.get('/hello/world(/*)?' /* ... */)

    Here is a fully working example, feel free to copy and paste this into a .js file to run with node, and play with it in a browser (or curl):

    const app = require('express')()
    
    // will be able to match all of the following
    const test1 = 'http://localhost:3000/hello/world'
    const test2 = 'http://localhost:3000/hello/world/'
    const test3 = 'http://localhost:3000/hello/world/with/more/stuff'
    
    // but fail at this one
    const failTest = 'http://localhost:3000/foo/world'
    
    app.get('/hello/world(/*)?', (req, res) => res.send(`
        This will match at example endpoints: <br><br>
        <pre><a href="${test1}">${test1}</a></pre>
        <pre><a href="${test2}">${test2}</a></pre>
        <pre><a href="${test3}">${test3}</a></pre>
    
        <br><br> Will NOT match at: <pre><a href="${failTest}">${failTest}</a></pre>
    `))
    
    app.listen(3000, () => console.log('Check this out in a browser at http://localhost:3000/hello/world!'))
    
    0 讨论(0)
  • 2020-12-02 16:58

    For those who are learning node/express (just like me): do not use wildcard routing if possible!

    I also wanted to implement the routing for GET /users/:id/whatever using wildcard routing. This is how I got here.

    More info: https://blog.praveen.science/wildcard-routing-is-an-anti-pattern/

    0 讨论(0)
  • 2020-12-02 16:59

    The connect router has now been removed (https://github.com/senchalabs/connect/issues/262), the author stating that you should use a framework on top of connect (like Express) for routing.

    Express currently treats app.get("/foo*") as app.get(/\/foo(.*)/), removing the need for two separate routes. This is in contrast to the previous answer (referring to the now removed connect router) which stated that "* in a path is replaced with .+".

    Update: Express now uses the "path-to-regexp" module (since Express 4.0.0) which maintains the same behavior in the version currently referenced. It's unclear to me whether the latest version of that module keeps the behavior, but for now this answer stands.

    0 讨论(0)
  • 2020-12-02 17:01

    I think you will have to have 2 routes. If you look at line 331 of the connect router the * in a path is replaced with .+ so will match 1 or more characters.

    https://github.com/senchalabs/connect/blob/master/lib/middleware/router.js

    If you have 2 routes that perform the same action you can do the following to keep it DRY.

    var express = require("express"),
        app = express.createServer();
    
    function fooRoute(req, res, next) {
      res.end("Foo Route\n");
    }
    
    app.get("/foo*", fooRoute);
    app.get("/foo", fooRoute);
    
    app.listen(3000);
    
    0 讨论(0)
  • 2020-12-02 17:06

    In array you also can use variables passing to req.params:

    app.get(["/:foo", "/:foo/:bar"], /* function */);
    
    0 讨论(0)
提交回复
热议问题