0

I know how http request works and I know how to send and receive response. This is the sample code of http request.

var http = require('http');    
var options = {
host: 'www.nodejitsu.com',
path: '/',    
port: '1337',    
method: 'POST'
};

callback = function(response) {
 var str = ''
 response.on('data', function (chunk) {
 str += chunk;
});

  response.on('end', function () {
   console.log(str);
  });      

}

var req = http.request(options, callback);

req.write("hello world!");
req.end();

In my site everything working fine Server B send request to server A and server A response to server B. But, one time I face a problem when there is huge no of traffic on server A and it was unable to receive any request from server B which halt the whole process.

So is there is any error block in request to handle this type of errors ? I googled alot and try this type of foolish things but it does not work for me

callback = function(response,error) {

 if(error){
   console.log(error)
 }else{
   var str = ''
   response.on('data', function (chunk) {
   str += chunk;
   });
   response.on('error', function (err) {
   console.log(err);
   }); 
 }
});

1 Answer 1

2

Have you tried this:

var req = http.request(options,callback);

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

as given here: http://nodejs.org/api/http.html#http_http_request_options_callback

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.