你可以使用express从reuqested route获取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' }
});
我想要的是返回path / to / file有没有办法得到它?或者它可能是我的app.get()路线错在这里?
我知道有很多方法可以使用正则表达式,分割,子串和许多其他方式使用纯JavaScript,但只是希望看到最好的方法.
解决方法:
您可以在req.params
中找到path / to / file:
When a regular expression is used for the route definition, capture groups are provided in the array using req.params[N], where N is the nth capture group. This rule is applied to unnamed wild-card matches with string routes such as
/file/*
:
// GET /get/path/to/file
req.params[0]
// => path/to/file
原文地址:https://codeday.me/bug/20190629/1321642.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。