node.js 如何在 Express 中获取完整的 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10183291/
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
How to get the full url in Express?
提问by Chris Abrams
Let's say my sample url is
假设我的示例网址是
and I say I have the following route
我说我有以下路线
app.get('/one/two', function (req, res) {
var url = req.url;
}
The value of urlwill be /one/two.
urlwill的值是/one/two。
How do I get the full urlin Express?
For example, in the case above, I would like to receive http://example.com/one/two.
如何在 Express 中获取完整 url?例如,在上述情况下,我想收到http://example.com/one/two.
回答by Peter Lyons
The protocol is available as
req.protocol. docs here- Before express 3.0, the protocol you can assume to be
httpunless you see thatreq.get('X-Forwarded-Protocol')is set and has the valuehttps, in which case you know that's your protocol
- Before express 3.0, the protocol you can assume to be
The host comes from
req.get('host')as Gopal has indicatedHopefully you don't need a non-standard port in your URLs, but if you did need to know it you'd have it in your application state because it's whatever you passed to
app.listenat server startup time. However, in the case of local development on a non-standard port, Chrome seems to include the port in the host header soreq.get('host')returnslocalhost:3000, for example. So at least for the cases of a production site on a standard port and browsing directly to your express app (without reverse proxy), thehostheader seems to do the right thing regarding the port in the URL.The path comes from
req.originalUrl(thanks @pgrassant). Note this DOES include the query string. docs here on req.url and req.originalUrl. Depending on what you intend to do with the URL,originalUrlmay or may not be the correct value as compared toreq.url.
该协议可作为
req.protocol. 文档在这里- 在 express 3.0 之前,您可以假设的协议,
http除非您看到它req.get('X-Forwarded-Protocol')已设置并具有 valuehttps,在这种情况下,您知道那是您的协议
- 在 express 3.0 之前,您可以假设的协议,
req.get('host')正如 Gopal 所指出的,主持人来自希望您的 URL 中不需要非标准端口,但如果您确实需要知道它,您将在应用程序状态中使用它,因为它是您
app.listen在服务器启动时传递给的任何内容。但是,在非标准端口上进行本地开发的情况下,Chrome 似乎将端口包含在主机标头中,因此req.get('host')返回localhost:3000,例如。因此,至少对于标准端口上的生产站点并直接浏览到您的 Express 应用程序(没有反向代理)的情况,host标题似乎对 URL 中的端口做了正确的事情。路径来自
req.originalUrl(感谢@pgrassant)。请注意,这确实包括查询字符串。req.url 和 req.originalUrl 上的文档。根据您打算对 URL 执行的操作,originalUrl与req.url.
Combine those all together to reconstruct the absolute URL.
将所有这些组合在一起以重建绝对 URL。
var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
回答by Michael Weibel
Instead of concatenating the things together on your own, you could instead use the node.js API for URLsand pass URL.format()the informations from express.
您可以将 node.js API 用于 URL并URL.format()从 express传递信息,而不是自己将这些内容连接在一起。
Example:
例子:
var url = require('url');
function fullUrl(req) {
return url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl
});
}
回答by chovy
I found it a bit of a PITA to get the requested url. I can't believe there's not an easier way in express. Should just be req.requested_url
我发现获取请求的 url 有点像 PITA。我不敢相信快递没有更简单的方法。应该只是 req.requested_url
But here's how I set it:
但我是这样设置的:
var port = req.app.settings.port || cfg.port;
res.locals.requested_url = req.protocol + '://' + req.host + ( port == 80 || port == 443 ? '' : ':'+port ) + req.path;
回答by mpolci
Using url.format:
使用url.format:
var url = require('url');
This support all protocols and include port number. If you don't have a query string in your originalUrl you can use this cleaner solution:
这支持所有协议并包括端口号。如果您的 originalUrl 中没有查询字符串,您可以使用这个更简洁的解决方案:
var requrl = url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl,
});
If you have a query string:
如果您有查询字符串:
var urlobj = url.parse(req.originalUrl);
urlobj.protocol = req.protocol;
urlobj.host = req.get('host');
var requrl = url.format(urlobj);
回答by lostintranslation
Here is a great way to add a function you can call on the req object to get the url
这是添加一个函数的好方法,您可以在 req 对象上调用以获取 url
app.use(function(req, res, next) {
req.getUrl = function() {
return req.protocol + "://" + req.get('host') + req.originalUrl;
}
return next();
});
Now you have a function you can call on demand if you need it.
现在您有了一个可以按需调用的函数。
回答by zhi.yang
make req.host/req.hostname effective must have two condition when Express behind proxies:
当Express 在代理后面时,使 req.host/req.hostname 生效必须有两个条件:
app.set('trust proxy', 'loopback');in app.jsX-Forwarded-Hostheader must specified by you own in webserver. eg. apache, nginx
app.set('trust proxy', 'loopback');在 app.js 中X-Forwarded-Host标头必须由您在网络服务器中指定。例如。阿帕奇,nginx
nginx:
nginx:
server {
listen myhost:80;
server_name myhost;
location / {
root /path/to/myapp/public;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myapp:8080;
}
}
apache:
阿帕奇:
<VirtualHost myhost:80>
ServerName myhost
DocumentRoot /path/to/myapp/public
ProxyPass / http://myapp:8080/
ProxyPassReverse / http://myapp:8080/
</VirtualHost>
回答by Vaghani Janak
Use this,
用这个,
var url = req.headers.host + '/' + req.url;
回答by mlibre
var full_address = req.protocol + "://" + req.headers.host + req.originalUrl;
or
或者
var full_address = req.protocol + "://" + req.headers.host + req.baseUrl;
回答by soomtong
My code looks like this,
我的代码看起来像这样,
params['host_url'] = req.protocol + '://' + req.headers.host + req.url;
params['host_url'] = req.protocol + '://' + req.headers.host + req.url;
回答by Amber Haq Dixon
I would suggest using originalUrl instead of URL:
我建议使用 originalUrl 而不是 URL:
var url = req.protocol + '://' + req.get('host') + req.originalUrl;
See the description of originalUrl here: http://expressjs.com/api.html#req.originalUrl
在这里查看 originalUrl 的描述:http: //expressjs.com/api.html#req.originalUrl
In our system, we do something like this, so originalUrl is important to us:
在我们的系统中,我们做这样的事情,所以 originalUrl 对我们很重要:
foo = express();
express().use('/foo', foo);
foo.use(require('/foo/blah_controller'));
blah_controller looks like this:
blah_controller 看起来像这样:
controller = express();
module.exports = controller;
controller.get('/bar/:barparam', function(req, res) { /* handler code */ });
So our URLs have the format:
所以我们的 URL 具有以下格式:
www.example.com/foo/bar/:barparam
Hence, we need req.originalUrl in the bar controller get handler.
因此,我们需要在 bar 控制器获取处理程序中使用 req.originalUrl。

