0

Variations of my issue have been asked dozens of times, but nothing I’ve tried worked.

My Node.js API works as expected on localhost for GET and POST requests.

On my EC2/nginx instance, the server block was correctly configured and served a static index over https.

Then I configured this server block as a proxy to my API port (8000) and it also returns that the API is listening.

Then I ran a simple GET /index endpoint that is routed and works correctly.

However, a similar POST /checkEmail endpoint (which, remember, works on localhost) here times out with a 504 error. This is where it gets weird.

It won’t even console.log the payload, the first line of the function. That means it isn’t even routed correctly. Here are my routes:

const API = require("./controllers/api");

module.exports = [
    { method: 'POST', path: '/checkEmail', options: API.checkEmail },
    { method: 'POST', path: '/sendEmail', options: API.sendEmail },
    { method: 'POST', path: '/recaptcha', options: API.recaptcha },
    { method: 'GET', path: '/index', options: API.index }
    
]

Since that the GET request returns the expected response, then it means all of these are true:

  • The firewall rules are letting traffic through the 443 (HTTPS) port
  • That traffic is proxied through the 8000 (API) port
  • The server block is serving the files correctly
  • The ssl certificate works
  • The request is sent to the upstream server

The endpoints:

const axios = require("axios");
const env   = require("dotenv").config();

const AWS = require("aws-sdk");

const subscriber = require("./middleware/subscriber");
const sanitizer  = require("./middleware/sanitizer");

exports.index = {
    cors: {
        origin: ["*"]
    },

    handler: (request, h) => {
        return "API Fluente";
    }
}

exports.checkEmail = {
    cors: {
        origin: ["*"]
    },
  
    handler: async (request, h) => {
        // This is never logged:
        console.log(request.payload);
        const payload = request.payload;
        const email = await sanitizer.email(payload.email);

        if(!email) throw new Error("Invalid email");

        const response = await verifyEmail(email);
        console.log("checkEmail attempt:", email, response.data)

        if (response.status === 200) {
            return h.response(response.data).code(200);
        }

    }
}

What I’ve tried:

  • Minimal/Full server block conf settings
  • curl the POST request from the EC2 CLI
  • Change ports from 8000 to something else (eg 8003)
  • Increasing timeout duration doesn’t make sense because these are simple requests that should return a response in a very short time.

None of these made any difference.

Felipe
  • 5
  • 3
  • 1
    this isn't really an EC2 or AWS problem if your EC2 instance is up and running. it's either an app problem or an nginx problem. I'm not really sure what kind of web framework you're using if any. but it sounds like it doesn't have the POST method configured properly. – bryan60 Mar 12 '22 at 18:29
  • I suppose nginx could be blocking POST requests somehow. It seems like the code is functional because I get responses on localhost and on other servers before. – Felipe Mar 12 '22 at 18:43
  • I would check the versions and configuration of all components that provide the service - I suspect there is an anomolly with the deployment on the EC2 instance, that you do not have locally or have not managed to replicate to an existing server. When in doubt, go back to basics, and have a game of "spot-the-difference" :-) – Android Control Mar 12 '22 at 19:24
  • I am not sure do did this, but try `console.log(request.payload);` to replace with a static log first like `console.log("Hello")`.. and see if that even works or not... if not then the problem is something else & then lets work on it.. first do this and share the result – Saksham Khurana Mar 13 '22 at 07:14

1 Answers1

0

Found the problem! As it turns out, it had nothing to do with AWS services or Node.

I was using the deprecated hapi package, when I should have been using @hapi/hapi.

Felipe
  • 5
  • 3