Search code examples
node.jsmongodbapiherokutimeout

Heroku timeout when calling my endpoint (H12), works perfectly on local server


Total noob here, and currently learning node.js and all the fun it can procure. I am wrote a REST API with node/express, with a database in mongoDB (and mongoose in my code). The database is a free one hosted on AWS (through the mongoDB website).

I am calling the following endpoint :https://rewaer-backend.herokuapp.com/api/users/ with postman.

Here is how the route look like:

// GET all users
router.get("/", async (req, res) => {
  try {
    const user = await User.find();
    res.json(user);
  }
  catch (err) {
    res.status(400).json({ message: err });
  }
});

but I a getting the following error from heroku:

2020-10-07T14:39:58.684608+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/api/users/" host=rewaer-backend.herokuapp.com request_id=94d50342-514e-45e7-94ad-5cb1acde44fa fwd="37.120.11.242" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https

My collection "users" has only two set of data. Also, really small. When running the server in local (connected to the DB on AWS) this works fine. As you can see in my code, I should be returning something, even if an err happens. So no reason to timeout here. The Heroku logs show that the connection to the db is successful.

I have no idea what is not working here, and google was not able to help me any further: hence me asking the real wise people ;)

Thanks a lot for any help,

Clément


Solution

  • The default server selection timeout in MongoDB drivers is 30 seconds. Heroku also has a 30 second timeout in its routing layer for the app server to produce the response.

    • Change server selection timeout to a lower value.
    • Then fix MongoDB connectivity issues you have (likely IP whitelist).