假设我的示例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。


当前回答

The protocol is available as req.protocol. docs here Before express 3.0, the protocol you can assume to be http unless you see that req.get('X-Forwarded-Protocol') is set and has the value https, in which case you know that's your protocol The host comes from req.get('host') as Gopal has indicated Hopefully 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.listen at 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 so req.get('host') returns localhost: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), the host header 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, originalUrl may or may not be the correct value as compared to req.url.

将这些组合在一起以重建绝对URL。

  var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;

其他回答

2021年

上面的答案工作得很好,但不是首选的文档,因为url。解析现在是遗留的,所以我建议你使用新的URL()函数,如果你想获得更多的控制URL。

表达方式

您可以从下面的代码获得完整的URL。

`${req.protocol}://${req.get('host')}${req.originalUrl}`

示例URL: http://localhost:5000/a/b/c?d=true&e=true#f=false

固定属性(你将在所有路线上得到相同的结果)

req.protocol: http
req.hostname: localhost
req.get('Host'): localhost:5000
req.originalUrl: /a/b/c?d=true&e=true
req.query: { d: 'true', e: 'true' }

非固定属性(将在每条路由中改变,因为它由express自己控制)

路线:/

req.baseUrl: <blank>
req.url: /a/b/c?d=true&e=true
req.path: /a/b/c

路线/

req.baseUrl: /a
req.url: /b/c?d=true&e=true
req.path: /b/c

文档:http://expressjs.com/en/api.html req.baseUrl

URL封装方式

在URL函数中,您将在每个路由中得到相同的结果,因此属性总是固定的。

属性

const url = new URL(`${req.protocol}://${req.get('host')}${req.originalUrl}`);
console.log(url)

您将得到如下结果。我根据图像改变了属性的顺序,这样它就可以匹配图像流。

URL {
  href: 'http://localhost:5000/a/b/c?d=true&e=true',
  protocol: 'http:',
  username: '',
  password: '',
  hostname: 'localhost',
  port: '5000',
  host: 'localhost:5000',
  origin: 'http://localhost:5000',
  pathname: '/a/b/c',
  search: '?d=true&e=true',
  searchParams: URLSearchParams { 'd' => 'true', 'e' => 'true' },
  hash: ''
}

注意:散列不能发送到服务器,因为它在服务器中被视为片段,但你会在客户端浏览器中获得。

文档:https://nodejs.org/api/url.html url_new_url_input_base

我发现它有点PITA,以获得所请求的url。我不敢相信没有更简单的快递方式了。应该是req。requested_url

但我是这样设置的:

var port = req.app.settings.port || cfg.port;
res.locals.requested_url = req.protocol + '://' + req.host  + ( port == 80 || port == 443 ? '' : ':'+port ) + req.path;

你可以从req of express得到完整的url。

function fetchPages(req, res, next) {

    let fullUrl = req.headers.host + req.originalUrl;
    console.log("full url ==> ",fullUrl);
}

The protocol is available as req.protocol. docs here Before express 3.0, the protocol you can assume to be http unless you see that req.get('X-Forwarded-Protocol') is set and has the value https, in which case you know that's your protocol The host comes from req.get('host') as Gopal has indicated Hopefully 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.listen at 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 so req.get('host') returns localhost: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), the host header 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, originalUrl may or may not be the correct value as compared to req.url.

将这些组合在一起以重建绝对URL。

  var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;

我的代码是这样的,

params['host_url'] = req.protocol + '://' + req.headers.host + req.url;