Express-js通配符路由,涵盖路径下的所有内容

Kit*_*nde 82 node.js express

我正试图让一条路线覆盖/foo包括/foo它自己在内的一切.我已经尝试过使用/foo*哪些工作除了它不匹配/foo.注意:

var express = require("express"),
    app = express.createServer();

app.get("/foo*", function(req, res, next){
  res.write("Foo*\n");
  next();
});

app.get("/foo", function(req, res){
  res.end("Foo\n");
});

app.get("/foo/bar", function(req, res){
  res.end("Foo Bar\n");
});

app.listen(3000);
Run Code Online (Sandbox Code Playgroud)

输出:

$ curl localhost:3000/foo
Foo
$ curl localhost:3000/foo/bar
Foo*
Foo Bar
Run Code Online (Sandbox Code Playgroud)

我有什么选择?我提出的最好的方法是路由/fo*当然不是最优的,因为它太匹配了.

ser*_*rby 101

我想你必须有2条路线.如果查看连接路由器的第331行,路径中的*将替换为.+ so将匹配1个或多个字符.

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

如果您有2条路线执行相同的操作,您可以执行以下操作以使其保持干燥状态.

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);
Run Code Online (Sandbox Code Playgroud)

  • `app.get(["/ foo","/ foo*"],/*function*/);`也可能是优惠的! (93认同)
  • 传入一串字符串也是我的优先选择.不幸的是:不推荐将数组传递给app.VERB(),将在4.0中删除 (9认同)
  • Express 4.9.1中已恢复传入字符串数组. (9认同)

Joh*_*ann 30

连接路由器现已被删除(https://github.com/senchalabs/connect/issues/262),作者声称你应该在连接(如Express)之上使用框架进行路由.

表示目前会将 app.get("/foo*")作为app.get(/\/foo(.*)/),消除了对两个单独的路由需求.这与之前的答案(指现在已删除的连接路由器)形成对比,后者表示" *在路径中替换为.+".

更新: Express现在使用"path-to-regexp"模块(自Express 4.0.0起),该模块在当前引用的版本中保持相同的行为.我不清楚该模块的最新版本是否能保持这种行为,但现在这个答案代表了.

  • 如果要匹配`/ foo`后跟一个或多个字符,请使用`/\/ foo(.+)/`,如果你想匹配,请使用`/ foo*`或`/\/ foo(.*)/`匹配`/ foo`后跟零个或多个字符. (2认同)

小智 8

在数组中,您还可以使用传递给req.params的变量:

app.get(["/:foo", "/:foo/:bar"], /* function */);
Run Code Online (Sandbox Code Playgroud)


Som*_* S. 5

不必有两条路线。

只需(/*)?path字符串末尾添加即可。

例如, app.get('/hello/world(/*)?' /* ... */)

这是一个完整的示例,可以将其复制并粘贴到.js文件中以与node一起运行,然后在浏览器(或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!'))
Run Code Online (Sandbox Code Playgroud)