如何将传入的 http 调用传递到 dart 中的另一台服务器?

How to pass through a incomming http call to another server in dart?

我有一个 Dart HTTP 服务 运行,如果该服务发布了一个文件,我想将此调用传递给另一个服务。我认为管道是正确的方式,但我不知道我可以这样做吗?!

管道是命令行 shell 功能。

当您的服务器收到请求时,它可以创建一个新请求到 "another server" 并将从传入请求接收到的数据发送到此 "another server"。

shelf_proxy 可能会帮助你做到这一点。

如果您收到传入请求,则客户端与您的服务器建立了 TCP 连接。您不能将其转发到另一台服务器。

您可以做的是 return 一个 301 重定向响应,它应该让请求者尝试使用其他服务器。

另一种方法是您自己从其他服务器获取数据并将其作为对原始请求的响应传递。您可以为此使用流管道。

类似于:

Future handleRequest(HttpRequest request) {
  return new HttpClient()  // Or reuse an existing client if it happens often
      .getUrl(createOtherServerUrl(request))
      .then((newRequest) => newRequest.close())
      .then((response) => response.pipe(request.response));
}