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

时间:2019-12-31 04:53:24

标签: javascript rust rust-rocket

我的机器上的rust服务器正在本地主机的端口4200上运行。我试图使用使用axios库的JavaScript客户端向此服务器发出请求。

代码在运行时会出现以下错误:

  

错误:连接ECONNREFUSED 127.0.0.1:4200       在TCPConnectWrap.afterConnect上[完成时](net.js:1191:14)

我尝试重写代码以使用访存库。那也返回连接被拒绝的错误。

从Postman尝试时,API可以按要求工作。 Get调用也在浏览器中运行。从JavaScript调用时,无法确定为什么此调用拒绝连接。

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

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

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

编辑:

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

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));
在我的计算机上正在运行的

浏览器请求: http://localhost:4200/createOffer/1/license

1 个答案:

答案 0 :(得分:4)

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

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

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

示例:

TblStyle