标签云

微信群

扫码加入我们

WeChat QR Code

Let's say my sample url ishttp://example.com/one/twoand I say I have the following routeapp.get('/one/two', function (req, res) {var url = req.url;}The value of url will be /one/two.How do I get the full url in Express? For example, in the case above, I would like to receive http://example.com/one/two.


FYI you can inspect the request object and look through but I am a hypocrite and found it on here.

2019年08月19日51分10秒

dave a client can send whatever headers it wants (as well as whatever URL, port, random non-HTTP garbage), however, at some point bogus or inaccurate headers will simply cause the protocol to fail. For example, in a virtual host environment, an incorrect "host" header will display a completely different web site. In the case of X-Forwarded-Protocol, that is usually not send by the actual client (browser) but by the reverse proxy (nginx/varnish/apache/whatever) that is serving HTTPS in front of your application.

2019年08月19日51分10秒

Or use req.get('Host') instead of req.host which gives the host plus the port section.

2019年08月19日51分10秒

-vote :the originalUrl should be used instead of the req.url

2019年08月19日51分10秒

The host parameter in the request headers can be spoofed. There's a possible "host header attack" if using the res.host this way. In the Django framework they have an 'allowed hosts' variable that is used to prevent such attack. I use a configuration variable that is my root_url which can be added to the req.url for completion. About the attack: skeletonscribe.net/2013/05/…

2019年08月19日51分10秒

If you want to add req.originalUrl without parameters, simply do req.originalUrl.split("?").shift(). Source: stackoverflow.com/a/32118818/2037431

2019年08月20日51分10秒

In my case the req.get('host') returns only the hostname part, not the port. Don't know why, but now I gather the port number from the settings, and use the hostname field, instead of host.

2019年08月19日51分10秒

Instead of pathname, I think you mean path. Which includes search/querystring

2019年08月19日51分10秒

This doesn't work for URL's that have a query string.

2019年08月19日51分10秒

port variable has to be defined?

2019年08月19日51分10秒

woops, i just edited it to use req.port;

2019年08月19日51分10秒

Does req.port exist? It is not in the Express documentation?

2019年08月19日51分10秒

My bad. I assumed you would know what port you're serving off of and set that prior. I'll update the example again. You can also get it with req.app.settings.port

2019年08月19日51分10秒

No require('url'), code is wrong.

2019年08月19日51分10秒

This doesn't include the user:password that you can get in a full url 'user:passhost.com:8080/p/a/t/h?query=string#hash'

2019年08月19日51分10秒

CodeUniquely true, but since that convention has been deprecated for over a decade now, hopefully no one is actually building userinfo specs into their APIs

2019年08月19日51分10秒

No require('url'), code is wrong.

2019年08月19日51分10秒

the example from: nginx.com/resources/wiki/start/topics/examples/likeapache

2019年08月19日51分10秒

yep, nobody told that, if you have no correct configuration in apache or nginx, you will get 127.0.0.1 from req.get('host')

2019年08月19日51分10秒

That gets me everything but the protocol...is there anything that can tell me that?

2019年08月19日51分10秒

To get the protocol use: req.protocol

2019年08月19日51分10秒

req.originalUrl is in this case /one/two but he wants http://example.com/one/two

2019年08月19日51分10秒