How can I get a vue application to talk an express api on my droplet?

February 19, 2019 446 views
API Deployment Node.js Ubuntu 18.04

On my local I just run the express app on localhost port 4000 and my vue app can connect using axios to localhost:4000/api/myendpoint.

On the droplet I built the vue app into a static dist folder and set it up with nginx and I have the express app running on localhost port 4000. So, now the frontend is properly displayed, but when it tries to make an api call to the express app I get “net::ERRCONNECTIONREFUSED”.

Here are the configurations I’ve tried:

Using curl works with both http://localhost:4000/path and http://0.0.0.0:4000/path when the express server is on 127.0.0.1 or 0.0.0.0.

1 Answer

Hey friend,

Interesting problem. Though things can be more complex, they rarely are. I like to take a step back and think of something so crazy simple that we’d usually look well past it. If the app on the server is listening on 127.0.0.1 port 4000, and it responds on 127.0.0.1 port 4000 to a local curl request, while the app receives a “Connection Refused” error, then I propose that the app is not trying to connect locally to the internal IP/port.

The reason I arrive there is that my other ideas fall short quickly:

  1. Firewall. If you can curl it, the firewall setting to block the app but not yourself is far too complex to have accidentally bumped into it (UID blocking I’m thinking, never seen it by accident).
  2. Linux user account lacking ability to make connection. I don’t even know how to do this, I just assume it’s theoretically possible. My lack of knowledge of how to do it is a testament to the fact that I’ve never witnessed it.

This leads me to what I think is the simplest answer, that the app isn’t doing what you think it’s doing. I could be wrong, it’s just where my mind keeps going. If you can grab the process ID of the app running when it processes the request, then you can try an strace to really see exactly what it’s doing. I really like this article for learning how to use/read strace:

https://www.redpill-linpro.com/sysadvent/2015/12/10/introduction-to-strace.html

Jarland

Have another answer? Share your knowledge.