首页 > 解决方案 > NextJS 在 AWS Elasticbeanstalk 上返回 502 错误

问题描述

我正在尝试使用 Codepipeline 在 AWS Elasticbeanstalk 上配置我的 nextjs 项目。我已经安装了 Express.js 来使用 EBS 部署它。

我的 server.js(在根文件夹中)

const express = require('express');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const port = process.env.PORT || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare()
.then(() => {
  const server = express();
  server.get('/test', (req, res) => {
    res.send('Hello from express!', 200);
  });
  server.get('*', (req, res) => {
    return handle(req, res);
  });
    
  server.listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${port}`);
  });
})
.catch((ex) => {
  console.error(ex.stack);
  process.exit(1);
});

这是我的 CodeBuild 的 buildspec.yml:

版本:0.2

phases:
  install:
    commands:
      - echo Installing dependency...
      - npm install -g next
      - yarn
  pre_build:
    commands:
      - printenv > .env
  build:
    commands:
      - echo Compiling the Node.js code
      - yarn build
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - '**/*'

最后是我的 package.json 文件;

"scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "next start",
    "deploy": "NODE_ENV=production next build && NODE_ENV=production node ./server.js",
    "analyze": "cross-env ANALYZE=true next build",
    "analyze:server": "cross-env BUNDLE_ANALYZE=server next build",
    "analyze:browser": "cross-env BUNDLE_ANALYZE=browser next build"
  },

那个配置有什么问题。有什么想法吗?

标签: amazon-web-servicesexpressnext.jsamazon-elastic-beanstalk

解决方案


推荐阅读