Handling request error

六眼飞鱼酱① 提交于 2019-12-11 17:49:53

问题


I'm modifying a script that makes request to URLs stored inside CSV file. Some of the links are broken or they no longer exists. I need to filter out those links so that node would continue on with good links and would not crash. How should I handle this error so that node wouldn't crash? This is a snippet of my code

var req = ref.protocol.get(options, function(res) {        
  res.on('data', function(data) {      
    f.write(data);
  }).on('end', function() {
    f.end();
  });
});

req.on('response', function(err) {
  if(err.statusCode == '404') { 
    console.log("Response ", err);
  }
}); 

req.on('error', function(err) {
  console.log("This is error ----" + err);
});
req.end();

Edit : After fiddling around with the code, I found that Node crashes only on 302 status. It does not seem to have problem with 404.

来源:https://stackoverflow.com/questions/25268296/handling-request-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!