是否可以检查express.js 处理程序是否正在使用HTTPS?

发布于 2024-12-01 03:46:44 字数 101 浏览 1 评论 0原文

有没有办法确定请求是否在带有express的node.js中使用HTTPS?我正在使用 Heroku 及其证书,我假设这些证书安装在负载均衡器上,而不是安装在单个 Web 服务器/实例上。

Is there a way to determine if the request is using HTTPS in node.js with express? I'm using Heroku with their certs which I'm assuming are installed at the load balancer and not on individual web servers/ instances.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-12-08 03:46:44

如果您可以配置 nginx,请在 nginx 配置中添加此参数

proxy_set_header X-Forwarded-Proto https;

,然后在代码中检查它并为其他处理程序设置标志。

mainapp.use(function(req, res) {

  if ( req.header('X-Forwaded-Proto') == 'https' ) {
     req.is_ssl = true;
  }
});

If you can configure nginx, add this parameter in nginx config

proxy_set_header X-Forwarded-Proto https;

And then in your code check for it and set flag for other handlers.

mainapp.use(function(req, res) {

  if ( req.header('X-Forwaded-Proto') == 'https' ) {
     req.is_ssl = true;
  }
});
时光暖心i 2024-12-08 03:46:44

Express 对 http 和 https 服务器有不同的配置。您是否在同一个应用程序中收听两者?或者只是听一个?

您可以查看 Express 指南以了解如何设置每种服务器类型
http://expressjs.com/guide.html

您可以设置 app = http 和 app2 = https 的内容如果您想从同一个文件中提供两者。

app.get('/server', function(req, res){ console.log('http 请求')});
app2.get('/server', function(req, res){ console.log('https 请求')});

Express has different configurations for http and https servers. Are you listening for both in the same app? or just listening for one?

You could check the Express guide for how to set up each server type
http://expressjs.com/guide.html

You could set something where app = http and app2 = https if you want to serve both from the same file.

app.get('/server', function(req, res){ console.log('http request')});
app2.get('/server', function(req, res){ console.log('https request')});

夏九 2024-12-08 03:46:44

同样的事情,在我的例子中,nginx 正在处理 ssl,因此我被迫获取客户端信息。在第一页加载时,我提取协议并将其发送到异步节点。

console.log(location.href.split('://')[0]);

认为这不是一个可靠的解决方案,但在生产中效果出奇的好。

您找到服务器端解决方案了吗?

Same thing here, nginx is handling ssl in my case, therefore I was forced to grab the info client-side. On first page load I extract the protocol and send it to node async.

console.log(location.href.split('://')[0]);

Thought that wouldn't be a reliable solution, but works surprisingly well in production.

Did you find a server-side solution yet?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文