将其他参数传递给node.js callack
我已经创建了一个rest客户端来在我的应用程序之外进行调用。我想将从rest调用收到的数据发送回客户端的web浏览器 类似于下面的内容,但是构造代码的最佳方式是什么,以允许以尽可能松耦合的方式访问写回web浏览器的响应?我不想在请求处理程序中定义rest客户机将其他参数传递给node.js callack,node.js,Node.js,我已经创建了一个rest客户端来在我的应用程序之外进行调用。我想将从rest调用收到的数据发送回客户端的web浏览器 类似于下面的内容,但是构造代码的最佳方式是什么,以允许以尽可能松耦合的方式访问写回web浏览器的响应?我不想在请求处理程序中定义rest客户机 var servReq = http.request(options, function(restResponse){ var status = restResponse.statusCode var headers =
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();
})
}
使用,您可以将API响应直接传递到应用程序的响应。这样,它将完全松散耦合,您的服务器将准确地返回API返回的内容
request(options).pipe(res);
感谢您的回复,这将提供我想要的优雅解决方案。