0

I have a rust server running in my machine at localhost, port:4200. I am trying to make requests to this server using a JavaScript client that uses axios library.

The code when run gives the following error:

Error: connect ECONNREFUSED 127.0.0.1:4200 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)

I tried rewriting the code to use fetch library. That is also returning a connection refused error.

The API is working as required when tried from Postman. The Get call is working from the browser as well. Could not find out why the connection is refused for this call when calling from JavaScript.

I have enabled CORS options in the rust server.

fn main() {
    let options = rocket_cors::Cors::default();

    rocket::ignite()
        .mount("/", routes![index, sign, generate])
        .attach(options)
        .launch();
}

EDIT:

client code that is giving above error when run from my machine:

const fetch = require("node-fetch");

var requestOptions = {
  method: "GET",
  headers: { "Content-Type": "application/json" }
};

fetch("http://localhost:4200/createOffer/1/license", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log("error", error));

browser request that is working from my machine: http://localhost:4200/createOffer/1/license

Sanjay S B
  • 259
  • 2
  • 15
  • Please provide the working (GET) and non-working code (fetch) too. Connection refused indicates that the client is connecting to a URL where nobody is listening, so you might actually use a different ip:port combination as target or you might try to connect from a different machine. I'm not sure that you aware that connections to localhost only work on the actual machine where the server is running and not on some other machine. – Steffen Ullrich Dec 31 '19 at 06:17
  • You can see in my question that the server is running on my machine. Will add the code. – Sanjay S B Dec 31 '19 at 06:19
  • *"You can see in my question that the server is running on my machine."* - Yes, you say that the server is running on your machine. But you don't say explicitly that the clients (browser, postman, your Javascript client) are running on the same machine too. – Steffen Ullrich Dec 31 '19 at 06:22
  • Code and error message don't really match. Code is using port 8000, error message and description of server say instead that port 4200 is used. Please explain this mismatch. – Steffen Ullrich Dec 31 '19 at 06:33
  • I have updated the server to run on different port. Still error persists. On any port the browser call works but the code with same url fails. If I try the same call from Postman it works too. Edited the question for uniformity. – Sanjay S B Dec 31 '19 at 06:36
  • You might check with strace (Linux) or similar tools on what your nodejs code is actually doing. Maybe you have a proxy configured which only affects the nodejs code (like as environment variable)? Maybe it is is physically the same machine but nodejs is running inside some container or similar (docker, lxc, virtual machine ...) where "localhost" is the container itself and not the interface the other tools consider localhost. Too much unknowns about your actual setup. – Steffen Ullrich Dec 31 '19 at 06:42
  • I am not using any sort of virtual machine. I am running the node js code and rust server from two different cmd windows which are on the same machine. The browser is also installed on the same machine and postman is also on the same machine. I am not using any proxy and there are no proxy related environment variables set. – Sanjay S B Dec 31 '19 at 06:47
  • Please check if your nodejs is able to access other URL (like google etc) to reduce the scope of your problem. – Steffen Ullrich Dec 31 '19 at 06:56
  • If I replace the localhost url with `http://dummy.restapiexample.com/api/v1/employees` it works. I get a response – Sanjay S B Dec 31 '19 at 06:59
  • This is really strange. Then I don't know either what the problem can be. – Steffen Ullrich Dec 31 '19 at 07:04
  • 1
    Have you resolved this issue? If you are using Rocket, I'd like to suggest configuring the server to listen on 127.0.0.1 instead of localhost. By default it seems that localhost maps only to ::1 which makes it inaccessible over ipv4. The answer posted by nwpointer touches on this. – Justin Jan 21 '20 at 21:33
  • I had not got the time to try this as I moved ahead using a temporary work around with curl and exec. using [::1] instead of localhost completely fine. – Sanjay S B Jan 22 '20 at 06:07

1 Answers1

5

Try hitting http://[::1] instead of http://localhost

I ran into a similar issue recently trying to do performance testing on Rocket. Rocket as of v0.4.2 does not seem to respond correctly to both ipv4 and ipv6 requests.

https://github.com/SergioBenitez/Rocket/issues/541 https://github.com/SergioBenitez/Rocket/issues/209

example:

const fetch = require("node-fetch");

var requestOptions = {
  method: "GET",
  headers: { "Content-Type": "application/json" }
};

fetch("http://[::1]:4200/createOffer/1/license", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log("error", error));
nwpointer
  • 76
  • 3