如果你想要在App Engine的Node.js应用程序中禁用HTTP重定向到HTTPS,你可以按照以下步骤进行操作:
app.yaml
文件中,将secure: always
注释或删除。这将禁用App Engine的自动HTTP重定向到HTTPS。示例app.yaml
文件:
runtime: nodejs14
#handlers:
#- url: /.*
# script: auto
# secure: always
示例代码:
const express = require('express');
const app = express();
// 中间件,将HTTP请求重定向到HTTPS
app.use((req, res, next) => {
if (req.headers['x-forwarded-proto'] !== 'https') {
res.redirect('https://' + req.hostname + req.url);
} else {
next();
}
});
// 添加你的路由和其他中间件
// ...
// 监听端口
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
在上面的代码中,我们使用Express框架来创建一个简单的Node.js应用程序。我们添加了一个中间件,它检查请求的x-forwarded-proto
标头,如果不是https
,则将请求重定向到相同的URL但使用HTTPS协议。
请注意,App Engine中的HTTP请求将通过x-forwarded-proto
标头传递所使用的协议。