node.js - HTTPS NodeJS 和 Heroku。强制使用 HTTPS?

标签 node.js ssl heroku https

我有一个在 Heroku 上运行的 NodeJS HTTP(非 S)服务器。我配置了 SSL,并且它接受对 HTTPS 的请求。我使用普通 HTTP 服务器的原因是 the following :

SSL termination occurs at Heroku's load balancers; they send your app plain (non-SSL) traffic, so your app should create a non-HTTPS server." 

不幸的是,我的应用程序仍然响应普通的 HTTP 请求。我想强制从 HTTP 重定向到 HTTPS。我可以使用一些中间件来做到这一点:

/* 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 */
})

但是这是一个好的解决方案吗? POST 请求如何工作?如果我发布到 HTTP,是否应该允许?

我想到的另一种 final方法是使用 Nginx 并在其中粘贴从 HTTP 到 HTTPS 的重定向。不幸的是 Heroku 不允许 Nginx 配置。

最佳答案

最好使用 app.use(function(req, res, next{/* ... */}); 来捕获其他 HTTP 方法。

关于node.js - HTTPS NodeJS 和 Heroku。强制使用 HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24643286/

相关文章:

javascript - 如何使用underscore.js对文本混合数字进行排序

node.js - Express-Session中 'secret'选项的核心功能是什么?

javascript - NodeJS https 服务器使用 express 返回 ERR_SSL_PROTOCOL_ERROR

ssl - IO : Protocol 'https' unsupported

algorithm - map 上许多点之间的距离

node.js - 从请求中获取用户浏览器数据

ssl - 用户数据约束不起作用

mysql - MySQL/Postgres 之间的大小写敏感问题

javascript - Node.js - events.js 抛出错误

php - 推送到 Heroku 应用程序失败(安装后 cmd 事件返回错误)- Composer 问题?