如何通过快递中的https检查请求是否已经过来

Mon*_*key 6 node.js express

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

我在heroku上使用piggyback ssl进行部署.

ant*_*ant 12

我也部署在Heroku上.当他们使用nginx反向代理时,他们会添加一堆标题.在这种情况下感兴趣的是x-forwarded-proto.

这就是我做的:

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
  }
});
Run Code Online (Sandbox Code Playgroud)