请求生锈火箭服务器从 JavaScript 应用程序拒绝错误连接

San*_*S B 0 javascript rust rust-rocket

我有一个 Rust 服务器在我的机器上运行,本地主机,端口:4200。我正在尝试使用使用 axios 库的 JavaScript 客户端向该服务器发出请求。

运行时的代码给出以下错误:

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

我尝试重写代码以使用 fetch 库。这也返回连接拒绝错误。

从 Postman 尝试时,API 按要求工作。Get 调用也在浏览器中工作。从 JavaScript 调用时,无法找出此调用的连接被拒绝的原因。

我在 Rust 服务器中启用了 CORS 选项。

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

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

编辑:

从我的机器运行时出现上述错误的客户端代码:

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));
Run Code Online (Sandbox Code Playgroud)

在我的机器上工作的浏览器请求: http://localhost:4200/createOffer/1/license

小智 5

尝试点击http://[::1]而不是http://localhost

我最近在尝试对 Rocket 进行性能测试时遇到了类似的问题。从 v0.4.2 开始,Rocket 似乎无法正确响应 ipv4 和 ipv6 请求。

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

例子:

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));
Run Code Online (Sandbox Code Playgroud)