2013-08-30 25 views
0

node.js express站點發生錯誤時,其日誌文件不顯示發生錯誤的URL。它顯示以前的請求的網址。但要調試錯誤,我需要知道導致錯誤的網址。如何記錄在node.js中導致錯誤的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?

+0

您可能能夠使用[錯誤處理中間件(http://expressjs.com/guide.html#error-handling ) – dc5

+0

thanx @ dc5,但是當使用[錯誤處理中間件](http://expressjs.com/guide.html#error-handling)我需要在任何地方調用'next()'我期望錯誤,但錯誤總是跳出你不希望它 –

回答

0

有一個簡單的代碼時,URL轉到不正確捕獲錯誤:

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

Thanx @Ashwin,但我使用[express](http://expressjs.com/api.html#req.route)和我的請求處理程序不同於你的例子:'app.get('/ user /:id?',function(req,res){console.log(req.route);});'。所以無法弄清楚如何在這裏使用'request.on' –