Node.js 是否可以将网络错误传递给客户端
我已经创建了一个http服务器,该服务器正在创建代理来向目标系统发送请求Node.js 是否可以将网络错误传递给客户端,node.js,Node.js,我已经创建了一个http服务器,该服务器正在创建代理来向目标系统发送请求 function sendErrorResponse(client_req,client_res){ var options = { : }; var proxy = http.request(options, function (res) { res.pipe(client_res, { end: true });
function sendErrorResponse(client_req,client_res){
var options = {
:
};
var proxy = http.request(options, function (res) {
res.pipe(client_res, {
end: true
});
});
client_req.pipe(proxy, {
end: true
});
}
或者使用http代理npm包
function sendErrorResponse(client_req,client_res){
proxy.web(client_req, client_res, { target: 'http://localhost:7777' });
}
现在,当客户机向我的服务器请求时,我将一些请求代理到另一台服务器,并将从目标服务器获得的内容发送到客户机。我希望,如果目标服务器给出一些错误(DNS解析、网络错误等),我可以将其发送到客户端,而不是使用带有错误消息的500
响应客户端
我该怎么做?为什么不使用错误回调,并根据需要格式化响应<代码>proxy.web(req,res,{target:'http://mytarget.com:8080“},函数(错误){//Here format the response with the error message})我试过了,但不起作用,你能给我一个例子,如何将相同的错误写入客户端请求吗?