【发布时间】:2023-03-09 15:05:02
【问题描述】:
我正在尝试让网站强制使用 HTTPS(从 HTTP 重定向)。我们已经通过 AWS Elastic Beanstalk 设置了 HTTPS。问题是目前HTTP和HTTPS都可以使用。
在阅读了一些帖子后,包括this one,下面的代码就是我想出的。不幸的是,这不起作用。
我错过了什么?
import express from 'express';
import { join } from 'path';
const app = express();
const buildPath = join(`${__dirname}/../build`);
const enforceHTTPS = (req, res, next) => {
if (req.headers['x-forwarded-proto'] === 'https') return next();
else return res.redirect(301, join(`https://${req.hostname}${req.url}`));
};
app.use(express.static(buildPath));
app.use(enforceHTTPS);
app.get('*', (req, res) => res.sendFile(`${buildPath}/index.html`));
app.listen(process.env.PORT || 3000, () => console.log('Server running on port 3000!'));
export default app;
【问题讨论】:
-
我认为您可以使用 aws 在您的节点端将所有 https 请求代理到 http。保持节点在 http 上运行。否则按照建议将 https 服务器与 express 一起使用。
标签: javascript node.js express https amazon-elastic-beanstalk