NodeJS HTTP post request read timeout

I am trying to execute the following code inside AWS Lambda which only makes an HTTP POST request to ElasticSearch.

The problem I'm running into is that it seems like the nodejs request has a read timeout and the response is almost always clipped and an error is thrown. I have verified that the issue is not related to AWS Lambda timeout which is set to 10 seconds and the code throws an error in less than a second.

As you can see, I tried to set the timeout to 5 seconds, but I think the connection timeout and not the read timeout.

What am I doing wrong?

var http = require('http');

exports.handler = (event, context, callback) => {

var options = {
    hostname: '172.31.40.10',
    port: 9200,
    path: '/articles/es/_search?_source=reference',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    }
};
var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (body) {
        var parsed = JSON.parse(body);
        var b = [];
        for (var i = 0; i < parsed.hits.hits.length; i++) {
            b.push(parsed.hits.hits[i]._source.reference);
        }
        var response = {
            statusCode: '200',
            body: JSON.stringify(b),
            headers: {
                'Content-Type': 'application/json',
            }
        };            
        callback(null, response);
    });
});
req.on('error', function(e) {
    callback(new Error('fallo'));
});
req.setTimeout(5000, function() {req.abort;})
req.on('socket', function (socket) {
    socket.setTimeout(5000);  
    socket.on('timeout', function() {
        req.abort();
    });
});    

req.write(MY_QUERY_HERE);
req.end();    
};

      

+3


source to share


1 answer


I think you should let the incoming data stream finish before doing any data manipulation.

Example:



var http = require('http');
//var _ = require('underscore');
function MyPostRequest(callback) {
 var options = {
  hostname:'172.31.40.10',
  port:9200,
  path:'/articles/es/_search?_source=reference',
  method:'POST',
  headers:{'Content-Type':'application/json'}
 };
 http.request(options, function(res) {
  var tmpstore = ''; //temp. data storage
  //:Store the continuous incoming data stream 
  res.on('data', function(d){tmpstore += d;});
  //:Data reception is done, use it now...
  res.on('end', function() {
    var parsed = JSON.parse(tmpstore); var b = [];
    for (var i = 0; i < parsed.hits.hits.length; i++) {
      b.push(parsed.hits.hits[i]._source.reference);
    }
/*  //I suggest using underscore module :
    _.each(parsed.hits.hits,function(element, index, list){
      b.push(element._source.reference);
    });
*/
    var response = {
      statusCode:'200',
      body:JSON.stringify(b),
      headers:{'Content-Type':'application/json'}
    };            
    callback(null, response);
  });
  //:Response contained an error
    res.on('error', function(e){/*error handling*/callback(e,null);});
 });
}

      

+1


source







All Articles