我有一个应用程序在Heroku上运行.js在 node 上.js和https.如何识别协议以强制重定向到https with Node.希罗库的js?

我的应用只是一个简单的http服务器,它(还)没有意识到Heroku正在向它发送https个请求:

// Heroku provides the port they want you on in this environment variable (hint: it's not 80)
app.listen(process.env.PORT || 3000);

推荐答案

答案是使用"x-forwarded-proto"的头,Heroku在代理thingamabob时向前传递该头.(旁注:它们还传递了几个可能很方便的x变量,check them out).

我的代码:

/* At the top, with other redirect methods before other routes */
app.get('*',function(req,res,next){
  if(req.headers['x-forwarded-proto']!='https')
    res.redirect('https://mypreferreddomain.com'+req.url)
  else
    next() /* Continue to other routes if we're not redirecting */
})

谢谢布兰登,我只是在等6个小时的延迟,不让我回答我自己的问题.

Node.js相关问答推荐

在Cypress测试文件中获取项目根路径

无法使用Sequelize连接AWS RDS

设置默认 node 版本

Mongoose抱怨说,整数是数字,而不是整数

AWS-ROUTE 53指向S3存储桶,错误是别名目标名称不在目标区域内

ForbidenError:使用Express.js的CSRF令牌无效

TS[2339]:类型 '() => Promise<(Document & Omit) | 上不存在属性空>'

合并Shift对象数组以创建最终的排班表

如何设置 Puppeteer Select 器的唯一性?

Amplify 部署的应用程序出现TypeError: handler is not a function错误,但它在本地运行

Typescript :泛型类又扩展了另一个泛型类

express 和 mongoose 的新密码不正确

在数组的另一个对象中获取数组的mongoose 对象

如何申请在NextJS上下载文件的许可?

如何在 Node.js 中使用 chmod

MongoDB Node findone如何处理没有结果?

如何从 npm 注册表中删除 npm 包?

node 应用程序是否有任何独立的 gui 模块

mongo 可以 upsert 数组数据吗?

我可以让 node --inspect 自动打开 Chrome