简体   繁体   中英

Node.js Http post request on aws lambda Socket Hang up

var http = require('http');
exports.handler = function(event, context) {

  var headers = {
      'content-type': 'application/x-www-form-urlencoded'
  }
  var options = {
    host: 'stage.wings.com',
    path:'/test-lambda',
    form: {
     'days':'3'
    },
    headers:headers
  };
  console.log(options);

  var req = http.request(options, function(response) {
      // Continuously update stream with data
      var body = '';
      response.on('data', function(d) {
          body += d;
      });
      response.on('end', function() {
          // Data reception is done, do whatever with it!
          var parsed = JSON.parse(body);
          console.log("success");
          console.log(parsed);
      });
  });
  // Handler for HTTP request errors.
  req.on('error', function (e) {
      console.error('HTTP error: ' + e.message);
      completedCallback('API request completed with error(s).');
  });
};

my node version : v0.10.25 If i execute on file it gives HTTP error: socket hang up From aws lambda if i run this function it throws error

Lambda error:2016-10-09T23:11:17.200Z 89f2146f-8e75-11e6-9219-b9b32aa0a768 Error: socket hang up at createHangUpError (_http_client.js:200:15) at Socket.socketOnEnd (_http_client.js:285:23) at emitNone (events.js:72:20) at Socket.emit (events.js:166:7) at endReadableNT (_stream_readable.js:905:12) at nextTickCallbackWith2Args (node.js:437:9) at process._tickDomainCallback (node.js:392:17)

There is a timeout time for aws-lambda, it will hang up after at most 300 seconds.

Here is little more about it. http://docs.aws.amazon.com/lambda/latest/dg/limits.html

you can use context.getRemainingTimeInMillis(); which will return you remaining time of your lambda so you can flush your data. If this is intended to be run longer than five minutes, then you can implement some kind of grace-full shutdown and flush your data before that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM