Express-js 通配符路由覆盖路径下的所有内容(包括路径)
我试图让一条路线覆盖 /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);
输出:
$ curl localhost:3000/foo
Foo
$ curl localhost:3000/foo/bar
Foo*
Foo Bar
我的选择是什么?我想出的最好的办法是路由 /fo*
这当然不是很理想,因为它会匹配太多。
I'm trying to have one route cover everything under /foo
including /foo
itself. I've tried using /foo*
which work for everything except it doesn't match /foo
. Observe:
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);
Outputs:
$ curl localhost:3000/foo
Foo
$ curl localhost:3000/foo/bar
Foo*
Foo Bar
What are my options? The best I've come up with is to route /fo*
which of course isn't very optimal as it would match way too much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想你必须有2条路线。如果查看连接路由器的第 331 行,路径中的 * 会替换为 .+,因此将匹配 1 个或多个字符。
https://github.com/senchalabs/connect/blob/master /lib/middleware/router.js
如果您有 2 个执行相同操作的路由,您可以执行以下操作来保留它 干。
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.
连接路由器现已删除 (https://github.com/senchalabs/connect/issues/262) ,作者指出您应该使用 connect 之上的框架(如 Express)进行路由。
Express 当前处理
app.get("/foo*")
为app.get(/\/foo(.*)/)
,删除需要两条单独的路线。这与之前的答案(指现已删除的连接路由器)形成对比,后者指出路径中的“*
被替换为.+
”。更新: Express 现在使用“path-to-regexp”模块(自 Express 4.0.0 起),该模块维护 与当前引用的版本中的行为相同。我不清楚该模块的最新版本是否保留了该行为,但目前这个答案仍然有效。
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*")
asapp.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.
这是一个完整的示例,请随意将其复制并粘贴到 .js 文件中以与节点一起运行,并在浏览器(或curl)中使用它:
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):
在数组中,您还可以使用传递给 req.params 的变量:
In array you also can use variables passing to req.params:
对于那些正在学习node/express的人(就像我一样):尽可能不要使用通配符路由!
我还想使用通配符路由实现 GET /users/:id/whatever 的路由。我就是这样来到这里的。
更多信息:https://blog.praveen.science/wildcard-路由是一种反模式/
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/