0

I can appreciate that this is quite a specific question, but I could really use some help because I am stuck for why this could be occurring.

I followed this Tutorial to upload my React / Express / Node app to AWS EC2.

You can see the Project Structure on Github. My server is in a different folder, but this shouldn't make a difference. Link to Github:

The front end works fine, and you can view it here.

It's the 'contact me' section at the bottom that I need to get to work. It links up with Nodemailer on the back-end to send me an email to my gmail.

Works fine in a development environment.

When I hit the api endpoint http://3.138.194.248/api/send with postman, I get back a message:

{
    "success": false,
    "message": "Something went wrong. Try again later"
}

Which I have set, so it would appear that the Express app is actually running.

I'm using pm2 and NGINX.

Environment variables are set up in a .env file in Ubuntu, exactly as they were in my development environment.

My NGINX config is as such

    server {
    listen 80;
    location / {
        proxy_pass http://{{PRIVATE IP FROM EC2 INSTANCE}}:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

Express app looks like so:

const path = require('path');

const cors = require('cors');

const express = require('express');

const app = express();

const buildPath = path.join(__dirname, '..', 'client/dist');

app.use(express.json());

app.use(express.static(buildPath));

app.use(cors());

const routes = require('./routes/api');

const port = process.env.PORT || 5000

app.listen(port, () => {
    console.log(`server start on port ${port}`);
})

app.use('/api', routes);

This seems to be giving me a 500 error in the console and it's not working anyhow.

Does anyone have some good troubleshooting steps that I can try or perhaps know why this might be happening?

Tim Iland
  • 27
  • 6
  • That error is produced by `transporter.sendMail()` in line 40 of your api route. Try logging the error is generates rather than a generic error message to find out what is wrong. Maybe you haven't set up the env variables for gmail? – Mark Feb 21 '21 at 18:23
  • Try doing the following 1. Open port 465 in the outbound in the ec2 security config. 2. Set up the smtp server in the config like the following var transporter = nodemailer.createTransport({ service: 'gmail', host: 'smtp.gmail.com', port: 465, secure: true, auth: { user: EMAIL_ID, pass: EMAIL_PASSOWRD, }, }); – Venkatesh A Feb 21 '21 at 18:58
  • Also take a look at https://stackoverflow.com/questions/55606781/nodemailer-is-causing-an-error-in-production-environment-on-aws-ec2 – Venkatesh A Feb 21 '21 at 19:01
  • I think this is an issue with Nodemailer on AWS. I have set up the outbound rules exactly as you suggest: "SMTPS TCP 465 0.0.0.0/0". I had already allowed less secure apps on Gmail, and I got a message from Gmail about an insecure sign in attempt as well. It's definitely not environment variables, because I've just typed the email and password in manually for the time being. – Tim Iland Feb 28 '21 at 19:20

0 Answers0