AWS Lambda - NodeJS POST request and asynch write/read file

偶尔善良 提交于 2019-12-30 10:36:13

问题


I am new to NodeJS and inside of AWS Lambda I am trying to make a POST request that calls an external API with a JSON object, creates a document with the response and then reads the contents of the file.

Coming from a Ruby background, I'm thinking the problem stems from my unfamiliarity with asynchronous programming, but I've tried using callbacks and readfileSync just to debug with no luck.

Any help would be appreciated.

var querystring = require('querystring');
var https = require('https');
var fs = require('fs');

exports.handler = function(event, context) {
  console.log('Received event:', JSON.stringify(event, null, 2));

  var operation = event.operation;
  delete event.operation;

  var accessKey = event.accessKey;
  delete event.accessKey;

  var templateName = event.templateName;
  delete event.templateName;

  var outputName = event.outputName;
  delete event.outputName;

  var req = {
    "accessKey": accessKey,
    "templateName": templateName,
    "outputName": outputName,
    "data": event.data
  };

  function doPost(data, callback) {
    // Build the post string from an object
    var post_data = JSON.stringify(data);

    // An object of options to indicate where to post to
    var post_options = {
        host: 'hostname.com',
        port: '443',
        path: '/path/to/api',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': post_data.length
        }
    };

    // Set up the request
    var file = fs.createWriteStream(outputName);

    var post_req = https.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.pipe(file);

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

        res.on('error', function(e) {
            context.fail('error:' + e.message);
        })

        res.on('end', function() {
            context.succeed('success, end request listener');
        });
    });

    // post the data
    post_req.write(post_data);
    post_req.end();
    callback();
  }

  function printFileContents() {
    fs.readFileSync(outputName, 'utf8', function (err, data) {
        console.log('file contents:' + data);
    });            
  }

  switch (operation) {
    case 'create':
        // Make sure there's data before we post it
        if(req) {
            doPost(req, printFileContents);
            printFileContents();
        }
        break;
     ...
  }
};

回答1:


In general, I'd recommend starting like this:

var querystring = require('querystring');
var https = require('https');
var fs = require('fs');

exports.handler = function(event, context) {
    console.info('Received event', event);

    var data = {
        "accessKey": accessKey,
        "templateName": templateName,
        "outputName": outputName,
        "data": event.data
    };

    // Build the post string from an object
    var post_data = JSON.stringify(data);

    // An object of options to indicate where to post to
    var post_options = {
        host: 'hostname.com',
        port: '443',
        path: '/path/to/api',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': post_data.length
        }
    };

    var post_request = https.request(post_options, function(res) {
        var body = '';

        res.on('data', function(chunk)  {
            body += chunk;
        });

        res.on('end', function() {
            context.done(body);
        });

        res.on('error', function(e) {
            context.fail('error:' + e.message);
        });
    });

    // post the data
    post_request.write(post_data);
    post_request.end();
};

You can see I simplified your code quite a bit. I'd recommend avoiding the file system since that would slow down your program. I'm also not sure about what is the real goal of your function so I just return the HTTP response.



来源:https://stackoverflow.com/questions/32636956/aws-lambda-nodejs-post-request-and-asynch-write-read-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!