简体   繁体   中英

Elastic Beanstalk Problem: Connection timing out when running my Node.js Express server

I'm trying to deploy my MERN app on Elastic Beanstalk, and I seem to be running into a final problem that I just cannot solve.

My app works fine when running my server locally (running node server), but when running on elastic beanstalk, the page never loads.

Upon inspection, the static elements are not being loaded, as seen in Dev Tools:

Image showing ERR_CONNECTION_TIMED_OUT in dev tools

I checked all the EB logs and did not find any errors or helpful messages. I'm thinking the problem is with EB not being able to find my static files somehow. It should however, my build files are not ignored by git and are deployed to EB.

Here's some background about my project: My backend and client code are in one project, with the following structure:

  • project
    • server.js
    • frontend
      • build
        • static
        • index.html

I run my app by building the react site, then running "node server" which runs great

Here is the relevent code from my server.js :

const port = process.env.PORT || 8081;

app.use(express.static(path.join(__dirname, 'frontend/build')));

app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'frontend/build/index.html'));
});

app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

The server is successfully sending logs that the server is running and that the database has established a connection. So it seems the server is fine, it's just that the front-end is the problem.

eb config file:

option_settings:
  aws:elasticbeanstalk:container:nodejs:
    NodeCommand: "npm start"
  aws:elasticbeanstalk:application:environment:
    PORT: 8081
    NODE_ENV: production
  aws:elasticbeanstalk:container:nodejs:staticfiles:
    /static: /frontend/build/static

I'm at a loss on how to solve this. The EB was deployed through the CLI and I haven't messed with any settings. I'm letting EB know where my static files are, and I believe it would say not found, rather than timing out.

Any help would be appreciated

Solved.

The problem was with using Helmet in my express server. I had ommited the code, thinking it not relevant, but here is the top portion of server.js, with the last line being the relevant portion:

const AWS = require('aws-sdk');
const cors = require('cors');
const express = require('express');
const helmet = require('helmet');
const mongoose = require('mongoose');
const path = require('path');
let Download = require('./models/Download.js');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 8081;

app.use(helmet());

Not using helmet solves the issue.

To be honest, I'm not sure why this is the problem. I assume that the problem is that helmet provides some security that my bare bones EB simply is not providing.

EDIT: Specifically, the problem is with CSP. Setting contentSecurityPolicy to false in Helmet is enough to fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM