【发布时间】:2018-04-27 18:03:51
【问题描述】:
我正在使用 expressjs 创建一个 http/https 服务器。
有一种情况,服务器无法从目录中获取公共和私有证书文件。
在这种情况下,我使用 http 创建服务器并将原始 HTML 文件发送到客户端,表明存在问题,但问题就在这里, 用户不知道他们需要移动到 http 网址而不是 https 才能查看 HTML 文件。
当我的用户尝试访问 http URL
时,有没有办法可以将我的用户重定向到 https urlinit.js
try {
// options = get public and private certificate file
} catch(e) {
// accessError
}
if(accessError) {
server = http.createServer();
} else {
server = https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
});
);
}
server.listen(8080);
app.js
let app = require('express');
app.use((req, res, next) => {
if(accessError) {
res.sendFile(index.html);
} else {
next();
}
});
index.html
<h1> there was an error <h1>
【问题讨论】:
-
如果没有有效的证书,浏览器不能(也不会)信任来自您服务器的任何数据,包括任何迁移到 http 的指示。这就是证书的基本用途。