Sanjay S B
Sanjay S B

Reputation: 259

Request to rust rocket server getting error connection refused from JavaScript app

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

Upvotes: 0

Views: 2424

Answers (1)

nwpointer
nwpointer

Reputation: 76

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));

Upvotes: 5

Related Questions