Search code examples
node.jsmongodbexpressnginxufw

How to connect node app to node api with Nginx


I built a Node app using this tutorial. Then I built a Node API using this tutorial. The app uses the app on port 4000 to connect to the API which then connects to a mongodb to store the info on the server.

This setup works great on my local machine, but I'm trying to deploy it on a digital ocean droplet. I have Nginx setup to listen to port 8080 for the main app. I'm able to navigate to the app. But when I try to register a user and submit the data to the API I get the following error in my browser OPTIONS http://localhost:4000/users/register net::ERR_CONNECTION_REFUSED.

I suspect I have to specify something in the Nginx config files. Or would it be a ufw issue? Any help would be much appreciated.


Solution

  • The error is very clear. The application try to fetch on localhost:4000, so you expect any visitor of your web app to have the API launched on their own computer.

    Change your code to point to the correct host and port of you server.

    Then, as you guess it, you will have to create a little Nginx configuration to tell him what to proxy to the APP and what to proxy to the API.