Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 是否可以将网络错误传递给客户端_Node.js - Fatal编程技术网

Node.js 是否可以将网络错误传递给客户端

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 });

我已经创建了一个http服务器,该服务器正在创建代理来向目标系统发送请求

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})我试过了,但不起作用,你能给我一个例子,如何将相同的错误写入客户端请求吗?