开发者

Is it possible to check if HTTPS is being used by an express.js handler?

Is there a way to determine if the request is using HTTPS in node.js with exp开发者_如何学编程ress? I'm using Heroku with their certs which I'm assuming are installed at the load balancer and not on individual web servers/ instances.


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;
  }
});


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')});


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜