Nodejs connection terminated unexpectedly

I’m attempting at deploying a Nodejs/React app onto fly and I’m met with an error message: Database connection failure ConnectionError [SequelizeConnectionError]: Connection terminated unexpectedly

The app-db dashboard logs first said I was reaching a ‘resource limit’ so I upgraded my db from the free tier packages which removed the health check log error but wasn’t the fix that I was looking for. I also checked to see if I missed setting a secret but that wasn’t the fix either.

Terminal error

fly.toml

Logs

Hi,

I’d take a look at the resource usage/graphs and see if there are any issues with e.g the memory or space for the database app that would prevent it from accepting connections.

Assuming not, I’d try connecting to the database from your local machine e.g via Wireguard, and check that works. That would confirm the database is working and the problem is with your app.

I’d then check to make sure the database’s hostname/database/user/password set for your app are all correct.

And check the database is within the same organisation as the app (to be on the same private network, else the hostname would not resolve).

Try and debug it step by step to see which part is the issue and take it from there.

Hi Greg, I appreciate the help.

  • I was able to connect to the database locally and everything checks out that the database is working and checked that the database is within the same org as my app.

  • I removed then re-added all secret envs .

  • After redeploying I’m still met with Health check on port 8080 has failed. Your app is not responding properly. Services exposed on ports [80, 443] will have intermittent failures until the health check passes.

Could this be due to my ports not being configured properly with the toml file? When I start my app locally I run my backend (Nodejs) on port 4000 while my frontend (React) runs on 3000 but after the build my frontend should be running on port 80.

1 Like

Hi,

No problem. Ah … sounds like a port issue. The default is 8080 and so if you are using a different one, Fly can’t connect, and hence the healthcheck error.

You could either change your app so it uses 8080 and not 4000, or change your fly.toml to instead tell Fly your app is using 4000. Whatever is easier.

e.g

1 Like

After updating the port to 4000 and also tried with 8080 I’m still met with the same health check error:

Health check on port 8080 has failed. Your app is not responding properly. Services exposed on ports [80, 443] will have intermittent failures until the health check passes.

Er … :thinking:

Well that’s odd!

I’m not sure what your healthcheck is but normally with React, that would be run client-side and the Node bit (often an API, if it’s a React SPA) would be server-side.

So if this value is 4000

… then this value would also need to be 4000:

That way your node app would be listening on the same port.

If yours is, and you still get an error, that would rule out the port and suggest something else is the issue. I’d e.g check the logs with fly logs to see if there are any other errors reported that would cause a healthcheck to fail. Like a path not existing? I tend to put a simple /healthcheck route in my Node apps for this purpose:

In order to pass health checks both of the first two parameters to listen must match - the first must match fly.toml, second must be either :: or 0.0.0.0.

without knowing the framework, it is difficult to make a specific suggestion. For example:

  • for express, check the parameters to listen
  • for fastify, instead of running npm start, call fastify start and pass “–address”, “0.0.0.0”
  • for gatsby, run “npx”, “gatsby”, “serve”, “-H”, “0.0.0.0”
  • for nuxt, set ENV HOST=0

I’m working on a dockerfile-node generator which attempts to recognize the framework and make adjustments like the above when possible.

1 Like

I’m running express 4.17.1

Yeah this error is really odd since the ports are matching up but after adding “::” to app.listen I’m still getting the same health check error and [SequelizeConnectionError]: Connection terminated unexpectedly

fly logs:

ok, so you are not even getting to the point where you are trying to listen. This code tries to connect to the database and only calls listen if that connection succeeded, which it did not.

An unfortunate consequence of JavaScript promises is that the stack tracebacks don’t tell you where to look, but someplace in ‘…/app/models’ you should have a line that reads new Sequelize(...). Can you share that code?

It looks like you are using postgres. If you are using fly’s postgres, that code could be as simple as new Sequalize(process.env.DATABASE_URL). If you are using a database from elsewhere, you might need to set an ssl option, see Using Heroku Postgres From a Fly App for an example.

1 Like

Updating the code to use new Sequalize(process.env.DATABASE_URL) on line 12 was the fix! I appreciate the help @greg @rubys I was finally able to connect to the database and deploy successfully.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.