如何在Hapi中获取请求的完整URL [英] How to get the full URL for a request in Hapi

查看:142
本文介绍了如何在Hapi中获取请求的完整URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的hapijs应用程序中,给定 Request对象,如何找到原始的,未解析的,未修改的URL ?

In my hapijs app, given a Request object, how can I find the original, unparsed, unmodified URL?

function getRequestUrl (request) {
    return ...; // What goes here?
}

我发现我可以从Request.info.hostRequest.pathRequest.query对其进行一些组合,但是它缺少该方案(即http vs https),并且有点不合时宜.普通网址在某处不可用吗?

I've found that I can piece it together somewhat from Request.info.host, Request.path, and Request.query, but it lacks the scheme (ie, http vs https), and is a bit of a kludge. Isn't the plain URL available somewhere?

推荐答案

完整的URL不会存储在您可以获取的地方.您需要从各个部分自己构建它:

The full URL isn't stored somewhere you can get it. You need to build it yourself from the parts:

const url = request.connection.info.protocol + '://' + request.info.host + request.url.path;

const url = request.connection.info.protocol + '://' + request.info.host + request.url.path;

即使它看起来很繁琐,但考虑一下它还是有道理的,因为没有原始的,未解析的,未修改的URL .通过网络进行的HTTP请求不包含在浏览器地址栏中键入的URL,例如:

Even though it might seem kludgey, it makes sense if you think about it because there is no original, unparsed, unmodified URL. The HTTP request that goes over the wire doesn't contain the URL as typed into the browser address bar for instance:

GET /hello?a=1&b=2 HTTP/1.1      // request.url.path
Host: localhost:4000             // request.info.host
Connection: keep-alive
Accept-Encoding: gzip, deflate, sdch
...

并且您仅基于hapi服务器连接是否处于TLS模式(request.connection.info.protocol)来了解协议.

And you only know the protocol based on whether the hapi server connection is in TLS mode or not (request.connection.info.protocol).

注意事项

如果您选择以下任何一项:

If you check either:

request.connection.info.urirequest.server.info.uri

报告的主机名将是服务器运行所在的实际计算机的主机名(* nix上hostname的输出).如果您想要实际的主机,请在浏览器中键入的人(可能有所不同),您需要检查request.info.host,该请求是从HTTP请求的Host标头解析的)

the reported hostname will be the hostname of the actual machine that the server is running on (the output of hostname on *nix). If you want the actual host the person typed in the browser (which might be different) you need to check request.info.host which is parsed from the HTTP request's Host header)

代理和X-Forwarded-Proto标头

如果您的请求是通过代理/负载均衡器/HTTPS终结器传递的,则很可能在HTTPS流量沿途某处被终止并通过HTTP连接发送到您的服务器,在这种情况下,您需要使用x-forwarded-proto标头的值(如果存在的话):

If your request got passed through a proxy(ies)/load balancers/HTTPS terminators, it's possible somewhere along the line HTTPS traffic got terminated and was sent to your server on an HTTP connection, in this case you'll want use the value of the x-forwarded-proto header if it's there:

const url = (request.headers['x-forwarded-proto'] || request.connection.info.protocol) + '://' + request.info.host + request.url.path;

const url = (request.headers['x-forwarded-proto'] || request.connection.info.protocol) + '://' + request.info.host + request.url.path;

带有模板字符串:

const url = `${request.headers['x-forwarded-proto'] || request.connection.info.protocol}://${request.info.host}${request.url.path}`;

这篇关于如何在Hapi中获取请求的完整URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆