当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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;

...

}

};

网友答案:

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 req = {
        "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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: