Ali*_*Ali 4 javascript node.js express
您可以使用express从 reuqested 路由中获取 node.js 中的其余路径吗?
假设我的服务器在端口上8080,我只是访问http://example.com:8080/get/path/to/file
var url = require("url");
app.get("/get/*", function(req, res) {
console.log(req.path);
// this will return
// '/get/path/to/file'
console.log(url.parse(req.path);
// this will return
// protocol: null,
// slashes: null,
// auth: null,
// host: null,
// port: null,
// hostname: null,
// hash: null,
// search: null,
// query: null,
// pathname: '/get/path/to/file',
// path: '/get/path/to/file',
// href: '/get/path/to/file' }
});
Run Code Online (Sandbox Code Playgroud)
我在这里想要的是返回path/to/file有没有办法得到它?还是我的app.get()路线错了?
我知道有办法用去做regex,split,substring和许多其他方式使用普通的JavaScript,但只是想看看去的最佳方式。
您可以path/to/file在req.params:
当路由定义使用正则表达式时,使用 req.params[N] 在数组中提供捕获组,其中 N 是第 n 个捕获组。此规则适用于具有字符串路由的未命名通配符匹配,例如
/file/*:
// GET /get/path/to/file
req.params[0]
// => path/to/file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2803 次 |
| 最近记录: |