How to catch http client request exceptions in node.js

http.createClient has been deprecated.

Here is a quick example of how to handle errors using the 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();

Leave a Comment