I successfully wrote app in node.js working with http.get. Problem is, that if page doesn't exist, it make an error that terminates the app. Is any way how to declare timeout. After timeout it should stop waiting for response and let app continue (if written synchronously) Thanks for all advices...
Asked
Active
Viewed 2,680 times
0
-
handle the error by giving `.on('error', function(e){});` See http://nodejs.org/api/http.html#http_http_get_options_callback – user568109 Jun 30 '13 at 17:39
1 Answers
0
I'm responding thinking that you are trying to retrieve info via http.get.
You can see the validate using the response status code.
http.get(url, function(res) { if (res.statusCode !== 200) { Your custom handler } else { var body = ''; res.on('data', function(chunk) { body += chunk; }); res.on('end', function() { console.log(body); }); } }).on('error', function(e) { console.log("error: ", e); });
A fully implemented example can be found her https://gist.github.com/pasupulaphani/9630789
You can set timeout and emit an error/abort if the request takes too long but this has to be done in application logic.
Found a better/sophisticated solution to handle timeouts in other post : https://stackoverflow.com/a/12815321/2073176