How to catch http client request exceptions in node.js -


i've got node.js app want use check if particular site , returning proper response code. want able catch errors come domain name isn't resolving or request timing out. problem is errors cause node crap out. i'm new whole asynchronous programming methodology, i'm not sure put try/catch statements.

i have ajax call goes /check/site1. server side calls function attempts make connection , return statuscode. it's simple function, , i've wrapped each line in try/catch , never catches anything. here is:

function checksite(url){     var site = http.createclient(80, url);     var request = site.request('get', '/', {'host': url});     request.end();     return request;   } 

even each of lines wrapped in try/catch, still uncaught exceptions ehostunreach , on. want able catch , return ajax call.

any recommendations on try next?

http.createclient has been deprecated.

here quick example of how handle errors using new http.request:

var http = require("http");  var options = {     host : "www.example.com" };  var request = http.request(options, function(req) {     ... }); request.on('error', function(err) {     // handle error });  request.end(); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -