扔掉带有代理的event.js http.get

时间:2013-04-02 11:45:38

标签: node.js http

    var tabProxy = new Array(
    '122.72.76.122'
    ,'124.240.187.79'
     );
 http.get ({
            host: tabProxy[selectorProxy],
            port: 80,
            path: baseurl + counter
        }, function (response) {
            try{
                var statusCode = response.statusCode;
                console.log (statusCode);
             }catch(e){
            console.log(e);
        }
    })

with selectorProxy = 0;这行得通 但是使用selectorProxy = 1; app crach:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: socket hang up
    at createHangUpError (http.js:1417:15)
    at Socket.socketOnEnd [as onend] (http.js:1513:23)
    at Socket.g (events.js:175:14)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:870:14
    at process._tickCallback (node.js:415:13)

如何重写这个错误(提取我的数组的这个ip)? try catch不起作用!!

1 个答案:

答案 0 :(得分:4)

您的try / catch不会涵盖任何会引发错误的内容。更重要的是,您需要根据请求收听'error'事件。

var req = http.get(options, function (res) { ... });
req.on('error', function (err) {
    //handle error here
});