我创建了一个休息客户端来在我的应用程序之外进行调用。我想将从其余调用中收到的数据发送回客户端的 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();
})
}