我如何检查一个请求是通过https快递 [英] how can I check that a request is coming over https in express

查看:136
本文介绍了我如何检查一个请求是通过https快递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想强制某些路线在我的快速应用程序中始终使用安全连接。我如何检查以确保它正在使用https?



我在英雄上使用搭载ssl进行部署。

解决方案

我也在Heroku部署。当他们使用nginx反向代理时,他们添加了一堆头文件。在这种情况下,感兴趣的是x-forwarding-proto。



这就是我所做的:

  app.get(/ \ / register $ /,function(req,res){
console.log(JSON.stringify(req.headers)); // to看到所有标题,heroku添加
如果(req.headers ['x -forward-proto']&& req.headers ['x-forwarding-proto'] ===http){
res.redirect(https://+ req.headers.host + req.url);
}
else {
//您处理此路由的其余逻辑
}
});


I want force certain routes to always use a secure connection in my express app. How can I check to make sure it is using https?

I am using piggyback ssl on heroku for my deployments.

解决方案

I deploy on Heroku as well. They add a bunch of their headers when they use nginx to reverse proxy. The one of interest in this case would be x-forwarded-proto.

This is what I did:

app.get(/\/register$/, function(req, res){
  console.log(JSON.stringify(req.headers)); //to see all headers that heroku adds
  if(req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] === "http") {
    res.redirect("https://" + req.headers.host + req.url);
  }
  else {
    //the rest of your logic to handle this route
  }
});

这篇关于我如何检查一个请求是通过https快递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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