2017-06-19 44 views
0

请求对象上出现了错误我在那里我请求对象使用如何发送回拨时的NodeJS

request.on('error', function (e) { 
      console.log("error while sending request: " + e); 
      }); 

捕捉错误的情况,但我对如何将这些错误发送到困惑客户端,我必须显示错误。我只能使用res.send();发送响应对象上的错误,任何帮助表示赞赏。我没有使用快递。

+0

请问Node.js Web服务器中是否发生“请求对象的catch错误”?我查看了https://nodejs.org/api/http.html文档,但没有找到请求对象的“错误”事件。 – shaochuancs

+0

@shaochuancs搜索http.request(options [,callback])# ,你可以找到它或尝试搜索.on('error' – NagaRajendra

+0

看来你是使用Node.js作为HTTP客户端,而不是服务器。 “发送此错误到客户端”是什么意思?错误在客户端... – shaochuancs

回答

0

如果你想返回响应对象的错误,浏览器。你可以使用如下代码:

const http = require('http'); 

let server = http.createServer(function(req, res) { 
    res.statusCode = 403; 
    res.statusMessage = 'Forbidden'; 
    res.write(JSON.stringify({message: 'something bad happens'})); 
    res.end(); 
}); 

server.listen(3000, function() { console.log("Server Listening on http://localhost:3000/"); });