如何在 node.js 中知道请求是 http 还是 https

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10348906/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:36:59  来源:igfitidea点击:

How to know if a request is http or https in node.js

node.jsexpress

提问by aartiles

I am using nodejs and expressjs. I wonder if there is something like request.headers.protocolin the clientRequest object. I would like to build the baseUrl for the web links. So if the request was done via https I would like to keep https in all links.

我正在使用 nodejs 和 expressjs。我想知道request.headers.protocolclientRequest 对象中是否有类似的东西。我想为 Web 链接构建 baseUrl。因此,如果请求是通过 https 完成的,我想在所有链接中保留 https。

    var baseUrl = request.headers.protocol + request.headers.host;

回答by Linus Gustav Larsson Thiel

Edit:For Express, it's safer and recommended to use req.secure(as @Andy recommends below). While it uses a similar implementation, it will be safe for future use and it also optionally supports the X-Forwarded-Protoheader.

编辑:对于 Express,它更安全,建议使用req.secure(正如@Andy 在下面推荐的那样)。虽然它使用了类似的实现,但将来使用是安全的,并且它还可以选择支持X-Forwarded-Proto标头。

That being said, for your use case it would be quicker to use Express' req.protocolproperty, which is either httpor https. Note, however, that for outgoing links, you can just refer to //example.com/path, and the browser will use the current protocol. (See also Can I change all my http:// links to just //?)

话虽如此,对于您的用例,使用 Express 的req.protocol属性会更快,即httphttps。但是请注意,对于传出链接,您可以只引用//example.com/path,浏览器将使用当前协议。(另请参阅我可以将所有 http:// 链接更改为仅 // 吗?

For node Requestobject without Express:

对于Request没有 Express 的节点对象:

It's in req.connection.secure(boolean).

它在req.connection.secure(布尔值)中。

Edit:The API has changed, for Node 0.6.15+:

编辑:API 已更改,适用于Node 0.6.15+

An HTTPS connection has req.connection.encrypted(an object with information about the SSL connection). An HTTP connection doesn't have req.connection.encrypted.

HTTPS 连接具有req.connection.encrypted(一个包含有关 SSL 连接信息的对象)。HTTP 连接没有req.connection.encrypted.

Also (from the docs):

另外(来自文档):

With HTTPS support, use request.connection.verifyPeer() and request.connection.getPeerCertificate() to obtain the client's authentication details.

使用 HTTPS 支持,使用 request.connection.verifyPeer() 和 request.connection.getPeerCertificate() 获取客户端的身份验证详细信息。

回答by Ben Sinclair

req.secureis a shorthand for req.protocol === 'https'should be what you looking for.

req.securereq.protocol === 'https'应为您要查找的内容的简写。

If you run your app behind proxy, enable 'trust proxy' so req.protocolreflects the protocol that's been used to communicate between client and proxy.

如果您在代理后运行您的应用程序,请启用“信任代理”以req.protocol反映用于在客户端和代理之间进行通信的协议。

app.enable('trust proxy');

app.enable('trust proxy');

回答by Gerardo Lima

You don't need to specify the protocol in URL, thus you don't need to bother with this problem.

你不需要在 URL 中指定协议,因此你不需要为这个问题而烦恼。

If you use <img src="//mysite.comm/images/image.jpg" />the browser will use HTTP if the page is served in HTTP, and will use HTTPS if the page is served in HTTPS. See the @Jukka K. Korpela explanationin another thread.

如果您使用<img src="//mysite.comm/images/image.jpg" />浏览器,如果页面以 HTTP 提供服务,则将使用 HTTP,如果页面以 HTTPS 提供服务,则将使用 HTTPS。请参阅另一个线程中的 @Jukka K. Korpela 解释

回答by Harsh

If you want to know whether request is http or https then use this in your code:

如果您想知道请求是 http 还是 https,请在您的代码中使用它:

req.headers.referer.split(':')[0];

This will return whether req is http or https.

这将返回 req 是 http 还是 https。

回答by tjklemz

For pure NodeJS (this works locally and deployed, e.g. behind Nginx):

对于纯 NodeJS(这在本地工作和部署,例如在 Nginx 后面):

function getProtocol (req) {
    var proto = req.connection.encrypted ? 'https' : 'http';
    // only do this if you trust the proxy
    proto = req.headers['x-forwarded-proto'] || proto;
    return proto.split(/\s*,\s*/)[0];
}

回答by user1910814

This worked for me:

这对我有用:

req.headers['x-forwarded-proto']

Hope this helped,

希望这有帮助,

E

回答by Nico

This is what works for me:

这对我有用:

getAPIHostAndPort = function(req, appendEndSlash) {
    return (req.connection && req.connection.encrypted ? 'https' : 'http') + '://' + req.headers.host + (appendEndSlash ? '/' : '');
}

回答by b4rtekb

If you are using request module and for example want to know what protocol does some www use, you can use: response.request.uri.protocol

如果您正在使用请求模块并且例如想知道某些 www 使用什么协议,您可以使用:response.request.uri.protocol

request(YOUR_TARGET, function(error, response, body){
    if (error){
        console.log(error);
    }
    else {
        console.log(response.request.uri.protocol); // will show HTTP or HTTPS
    }
});

If you need user protocol then use request.headers.referer.split(':')[0];just like @Harshgave you.

如果您需要用户协议,请使用request.headers.referer.split(':')[0]; 就像@Harsh给你的一样。

回答by MajidJafari

If you are using Proxy Server such as Nginx you should set proxy_set_header X-Forwarded-Proto https;in its config file, so if you are using TSLexpress could recognize httpsas req.headers['x-forwarded-proto']value or truefor req.secure

如果你使用代理服务器,比如 Nginx,你应该proxy_set_header X-Forwarded-Proto https;在它的配置文件中设置,所以如果你使用TSLexpress 可以识别httpsreq.headers['x-forwarded-proto']value 或trueforreq.secure