假设我的示例URL是
http://example.com/one/two
我说我有以下路线
app.get('/one/two', function (req, res) {
var url = req.url;
}
url的值是/one/two。
如何在Express中获得完整的URL ? 例如,在上面的情况下,我想收到http://example.com/one/two。
假设我的示例URL是
http://example.com/one/two
我说我有以下路线
app.get('/one/two', function (req, res) {
var url = req.url;
}
url的值是/one/two。
如何在Express中获得完整的URL ? 例如,在上面的情况下,我想收到http://example.com/one/two。
当前回答
你可以使用node.js API来获取url,并将来自express的信息传递给URL.format(),而不是自己将这些东西连接在一起。
例子:
var url = require('url');
function fullUrl(req) {
return url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl
});
}
其他回答
我的代码是这样的,
params['host_url'] = req.protocol + '://' + req.headers.host + req.url;
我试着把所有数据都记录下来以备不时之需
然后我发现日志rawHeaders和发现所有的数据可用的url
我试了一下
app.post("/news-letter", (req,res) => {
console.log(req.body);
res.redirect(req.rawHeaders[33]);
})
async function (request, response, next) {
const url = request.rawHeaders[9] + request.originalUrl;
//or
const url = request.headers.host + request.originalUrl;
}
你可以从req of express得到完整的url。
function fetchPages(req, res, next) {
let fullUrl = req.headers.host + req.originalUrl;
console.log("full url ==> ",fullUrl);
}
你需要使用req.headers.host + req.url来构造它。当然,如果你是在不同的端口,你得到的想法;-)