【问题标题】:Express.js routes responding with 504 error when deployed to AWS部署到 AWS 时,Express.js 路由响应 504 错误
【发布时间】:2017-10-17 04:34:43
【问题描述】:

我正在通过 AWS Elastic Beanstalk 为应用程序提供服务。 localhostngrok 按预期提供网页和其他 api 调用。当上传到AWS-ELB 时,504 error 是网页提供后任何其他 api 调用的响应。

应用程序通过以下方式提供基本 index.html 文件:

基本设置和服务索引.HTML

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.all('/', ensureSecure)
function ensureSecure(req, res, next){
  if(req.headers['x-forwarded-proto'] === 'https'){
    // OK, continue
    return next()
  };

   res.redirect('https://' + req.headers.host)
}
app.use(express.static(__dirname + '/../web'), () => {}) // SERVE THE WEBPAGE

var port = process.env.PORT || 443

var router = express.Router()

router.use(function(req, res, next) {

    console.log('here')
    res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    next(); // make sure we go to the next routes and don't stop here
});

然后它下面还有其他路线:

后续 API 调用

app.get('/user/:type', function(req, res) {
  res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

  var dbparams = {};
  dbparams.TableName = 'FFUserTable';
  dbparams.KeyConditionExpression = 'uid = :uid'
  dbparams.ExpressionAttributeValues = {':uid':req.query.uid};
  ddb_calls.awsDynamoQuery(dbparams, function(data){
    res.json(data);
  })

})

索引页面的服务和呈现都很好。但是当页面调用它们时,它下面的任何路由都会返回 504 网关错误。奇怪的是,这只发生在部署到 AWS-ELB 时。使用 localhost 或使用 ngrok 时,一切都按预期工作。即使我取出https 重定向,我也会遇到同样的问题。

AWS-ELB ssl 已通过 AWS-Route53 正确设置和路由。

【问题讨论】:

  • 504 表示“网关超时”,因此传递给您的 Node 应用程序的请求超时。您可以为您的问题超时的路线之一添加代码吗?
  • 已更新。除上述情况外,该路线在所有其他情况下都能正常工作。如果我将前端服务器和后端分离到单独的服务器上,它甚至可以在 ELB 上运行。现在我从同一台服务器为index.html 提供服务才开始失败。
  • 在我看来ddb_calls.awsDynamoQuery 可能会导致超时。
  • 不,不是这样。大约有十几个其他路线,并且无论路线内的代码如何,它都会发生在任何其他路线中。
  • 哦,等一下,我现在看到问题了...

标签: amazon-web-services express amazon-elastic-beanstalk


【解决方案1】:

这就是问题所在:

app.use(express.static(__dirname + '/../web'), () => {})

我不知道你为什么要在那里添加最后一个函数,但它是一个(不完整的)中间件函数,它会停止所有请求,因为它没有做任何事情。

应该是这样的:

app.use(express.static(__dirname + '/../web'))

【讨论】:

  • 宾果游戏。就是这样。也不知道我为什么这样做。感谢您的帮助!
猜你喜欢
  • 2017-09-02
  • 2022-10-01
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2020-04-15
  • 2020-04-20
相关资源
最近更新 更多