title: NextJS Returns 502 Eror on AWS Elasticbeanstalk
body: <p>I'm trying to configure my nextjs project on AWS Elasticbeanstalk  with Codepipeline.
  I've installed Express.js to deploy it with EBS.</p>
  <p>My server.js (on root folder)</p>
  <pre><code>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(() =&gt; {
    const server = express();
    server.get('/test', (req, res) =&gt; {
      res.send('Hello from express!', 200);
    });
    server.get('*', (req, res) =&gt; {
      return handle(req, res);
    });
      
    server.listen(port, (err) =&gt; {
      if (err) throw err;
      console.log(`&gt; Ready on http://localhost:${port}`);
    });
  })
  .catch((ex) =&gt; {
    console.error(ex.stack);
    process.exit(1);
  });
  </code></pre>
  <p>Here's my buildspec.yml for CodeBuild:</p>
  <p>version: 0.2</p>
  <pre><code>phases:
    install:
      commands:
        - echo Installing dependency...
        - npm install -g next
        - yarn
    pre_build:
      commands:
        - printenv &gt; .env
    build:
      commands:
        - echo Compiling the Node.js code
        - yarn build
    post_build:
      commands:
        - echo Build completed on `date`
  artifacts:
    files:
      - '**/*'
  </code></pre>
  <p>And finally my package.json file;</p>
  <pre><code>&quot;scripts&quot;: {
      &quot;dev&quot;: &quot;node server.js&quot;,
      &quot;build&quot;: &quot;next build&quot;,
      &quot;start&quot;: &quot;next start&quot;,
      &quot;deploy&quot;: &quot;NODE_ENV=production next build &amp;&amp; NODE_ENV=production node ./server.js&quot;,
      &quot;analyze&quot;: &quot;cross-env ANALYZE=true next build&quot;,
      &quot;analyze:server&quot;: &quot;cross-env BUNDLE_ANALYZE=server next build&quot;,
      &quot;analyze:browser&quot;: &quot;cross-env BUNDLE_ANALYZE=browser next build&quot;
    },
  </code></pre>
  <p>What's wrong with that configuration.
  Any idea for that?</p>