如何记录导致node.js表示错误的url

时间:2013-08-30 06:02:06

标签: node.js express

node.js express站点发生错误时,其日志文件不会显示发生错误的URL。它显示了先前请求的URL。但要调试错误,我需要知道导致错误的url。

GET /some/local/url?page=2 200 339ms - 68b  # previus request

http.js:704                                 # error request without url
    throw new Error('Can\'t set headers after they are sent.');
          ^
Error: Can't set headers after they are sent.
  at ServerResponse.OutgoingMessage.setHeader (http.js:704:11)

我使用forevernginx。那么如何让它显示在日志文件中导致错误的url?

1 个答案:

答案 0 :(得分:0)

当网址不正确时,有一个简单的代码可以捕获错误:

var http = require("http");

var options = {
    host : "www.abc.com/get/something"
};

var request = http.request(options, function(req) {
    // DO WITH REQUEST
});
request.on('error', function(err) {
     console.log(err.domain); // domain url
     console.log(request.url); // URL of req made 
    // DO SOMETHING WITH ERROR
});