使用 node.js/express 自动 HTTPS 连接/重定向 - Automatic HTTPS connection/redirect with node.js/express

10/14 12:49
阅读数 0

问题:

I've been trying to get HTTPS set up with a node.js project I'm working on.我一直在尝试使用我正在处理的 node.js 项目设置 HTTPS。 I've essentially followed the node.js documentation for this example:对于此示例,我基本上遵循了node.js 文档

// curl -k https://localhost:8000/
var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8000);

Now, when I do现在,当我做

curl -k https://localhost:8000/

I get我得到

hello world

as expected.正如预期的那样。 But if I do但如果我这样做

curl -k http://localhost:8000/

I get我得到

curl: (52) Empty reply from server

In retrospect this seems obvious that it would work this way, but at the same time, people who eventually visit my project aren't going to type in https ://yadayada, and I want all traffic to be https from the moment they hit the site.回想起来,这似乎很明显它会以这种方式工作,但与此同时,最终访问我的项目的人不会输入https ://yadayada,我希望从他们点击的那一刻起所有流量都是 https网站。

How can I get node (and Express as that is the framework I'm using) to hand off all incoming traffic to https, regardless of whether or not it was specified?我怎样才能让节点(和 Express 作为我正在使用的框架)将所有传入流量移交给 https,无论是否指定? I haven't been able to find any documentation that has addressed this.我找不到任何解决此问题的文档。 Or is it just assumed that in a production environment, node has something that sits in front of it (eg nginx) that handles this kind of redirection?或者只是假设在生产环境中,节点前面有一些东西(例如 nginx)来处理这种重定向?

This is my first foray into web development, so please forgive my ignorance if this is something obvious.这是我第一次涉足 Web 开发,所以如果这很明显,请原谅我的无知。


解决方案:

参考一: https://en.stackoom.com/question/VGKS
参考二: https://stackoom.com/question/VGKS
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部