node.js 带斜杠的 Express.js 路由参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16829803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express.js route parameter with slashes
提问by Gerstmann
I have an application which serves file listings.
我有一个提供文件列表的应用程序。
The application must respond to following routes:
应用程序必须响应以下路由:
/company/:id
/company/:id/dir
/company/:id/dir/dir
Here /company/:idis a route with no pathspecified e.g a rootdirectory. I was thinking for something like app.get('/company/:id/:path', ...which obviously doesn't work.
这/company/:id是一个没有path指定的路由,例如一个root目录。我正在考虑类似的事情app.get('/company/:id/:path', ...,但显然行不通。
How can I define a route which responds to all of the examples?
如何定义响应所有示例的路由?
回答by Prinzhorn
Use /company/:id*(note trailing asterisk).
使用/company/:id*(注意尾随星号)。
Full example
完整示例
var express = require('express')();
express.use(express.router);
express.get('/company/:id*', function(req, res, next) {
res.json({
id: req.params['id'],
path: req.params[0]
});
});
express.listen(8080);

