Reputation: 1981
I have code that hits a POST
URL and receives the data. Below is the code:
var postdata="{u:1}";
var options = {
host: 10.0.0.1,
port: 80,
path: /ddd,
method: post
};
var reqClient = http.request(options, function(res)
{
var data = '';
res.setEncoding('utf8');
});
//setting the request contenet type to application/json
reqClient.setHeader('Content-Type','application/json');
//Posts the data to the Server
reqClient.write(postData);
reqClient.end();
reqClient.on('response', function (res)
{
var data = '';
res.on('data', function (chunk)
{
data+=chunk;
});
res.on('end',function(){
//Perform some functions
});
}).on('error', function(){
//error handler
}); ;
I have to handle an error, say when the server is not able to hit the client URL. Can I follow the above approach? I am stuck here.
Upvotes: 0
Views: 2990
Reputation: 677
You would want to add another event listener for the error event which is detailed on the Node.js HTTP Documentation.
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
Upvotes: 1