1

我创建了一个休息客户端来在我的应用程序之外进行调用。我想将从其余调用中收到的数据发送回客户端的 Web 浏览器。

类似于以下内容,但是构建代码以允许访问响应以尽可能松散耦合写回 Web 浏览器的最佳方式是什么?我不想在请求处理程序中定义其余客户端。

var servReq = http.request(options, function(restResponse){
    var status = restResponse.statusCode
    var headers = restResponse.headers
    restResponse.setEncoding("utf8");
    d='';
    restResponse.on('data', function(chunk){
        d += chunk;
    })
    restResponse.on('end', function(restResponse){
        // res would be a response to write back to the client's web browser
        // with the data received from the rest client.
        res.writeHead(200, {"content-type":"text/plain"})    
        res.write(d)
        res.end();
    })
}
4

1 回答 1

0

使用request,您可以将 API 响应直接传送到应用程序的响应。这样,它将完全松散耦合——您的服务器将准确地返回 API 返回的内容。

request(options).pipe(res);

https://github.com/mikeal/request#streaming

于 2012-10-28T22:47:01.783 回答