如何配置expressjs来处理http和https?
我已经搜索过 stackoverflow 和express google group,但仍然不够。
根据我收集的信息,我可以执行以下两件事之一:
1)创建 http 服务器和 https 服务器的实例,并将两者设置为侦听两个不同的端口。在路由中,将 http 请求重定向到 https 端口。
//app
var app = express.createServer();
var app_secure = express.createServer({key: key, cert: cert});
app.listen(8080);
app_secure.listen(8443);
//routes
app.get("unsecure/path", function(req, res) {
...
}
app.get("secure/path", function(req, res) {
res.redirect("https://domain" + req.path);
}
app_secure.get("secure/path", function(req, res) {
res.send("secure page");
}
2)按照TJ Hollowaychuk所说的去做: https://gist.github.com/1051583
var http = require("http");
var https = require("https");
var app = express.createServer({key: key, cert: cert});
http.createServer(app.handle.bind(app)).listen(8080);
https.createServer(app.handle.bind(app)).listen(8443);
当我做1时,通常没有问题。然而,管理两台服务器感觉很笨重,我真的觉得应该有更好的方法。
当我执行 2 时,我得到这个:
(node SSL) error:1408A0C1:SSLroutines:SSL3_GET_CLIENT_HELLO:no share cipher
当然,我可以默认使用选项 1,但我真的非常想知道为什么我会得到这个当我执行选项 2 时,“没有共享密码错误”。选项 2 将是我的首选路线。
I've scoured stackoverflow and the express google group, but I'm still coming up short.
From what I gather, I can do one of two things:
1) create an instance of an http server and an https server and set the two to listen to two different ports. In the routes, redirect the http request to the https port.
//app
var app = express.createServer();
var app_secure = express.createServer({key: key, cert: cert});
app.listen(8080);
app_secure.listen(8443);
//routes
app.get("unsecure/path", function(req, res) {
...
}
app.get("secure/path", function(req, res) {
res.redirect("https://domain" + req.path);
}
app_secure.get("secure/path", function(req, res) {
res.send("secure page");
}
2) do what TJ Hollowaychuk says: https://gist.github.com/1051583
var http = require("http");
var https = require("https");
var app = express.createServer({key: key, cert: cert});
http.createServer(app.handle.bind(app)).listen(8080);
https.createServer(app.handle.bind(app)).listen(8443);
When I do 1, there are generally no problems. However, it feels clunky to manage two servers and I really feel like there should be a better way.
When I do 2, I get this:
(node SSL) error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher
Of course, I can just default to option 1, but I really, really want to know why I'm getting that "no shared cipher error" when I do option 2. And option 2 would be my preferred route.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照@ypocat的评论,您可以在express.js应用程序中启用https,如下所示。
请注意,您应该从证书颁发机构接收server.ca-bundle、server.key和server.crt。
另外,由于您可能会在没有 sudo 的情况下运行节点,因此您需要确保端口 80(http) 和 443(https) 已打开
,并分别将 8080 上的请求转发到 80 以及从 8081 转发到 443
希望这有帮助
Following @ypocat 's comment you can enable https in your express.js application like so
Note that you should receive server.ca-bundle, server.key and server.crt from a certificate authority.
Also as you will probably run node without sudo you need to make sure port 80(http) and 443(https) are open
and to forward requests on 8080 to 80 and from 8081 to 443 respectively
Hope this helps
您的证书是 RSA 证书而不是 DSA 证书吗?听起来您的 NodeJS 服务器不支持您的浏览器支持的密码 - 您是否需要更新 OpenSSL 并重新编译 NodeJS?
Is your certificate an RSA certificate rather than a DSA one? It sounds like the ciphers your browser supports are not supported by your nodejs server - you many need to update your OpenSSL and recompile NodeJS?