Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js AWS Lambda-NodeJS POST请求和异步写/读文件_Node.js_Amazon Web Services_Asynchronous_Http Post_Aws Lambda - Fatal编程技术网

Node.js AWS Lambda-NodeJS POST请求和异步写/读文件

Node.js AWS Lambda-NodeJS POST请求和异步写/读文件,node.js,amazon-web-services,asynchronous,http-post,aws-lambda,Node.js,Amazon Web Services,Asynchronous,Http Post,Aws Lambda,我是NodeJS新手,在AWS Lambda中,我试图发出一个POST请求,用JSON对象调用外部API,创建一个带有响应的文档,然后读取文件内容 来自Ruby的背景,我认为这个问题源于我对异步编程的不熟悉,但我尝试使用回调和readfileSync只是为了调试而没有运气 任何帮助都将不胜感激 var querystring = require('querystring'); var https = require('https'); var fs = require('fs'); expor

我是NodeJS新手,在AWS Lambda中,我试图发出一个POST请求,用JSON对象调用外部API,创建一个带有响应的文档,然后读取文件内容

来自Ruby的背景,我认为这个问题源于我对异步编程的不熟悉,但我尝试使用回调和readfileSync只是为了调试而没有运气

任何帮助都将不胜感激

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;
     ...
  }
};

一般来说,我建议这样开始:

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();
};

你可以看到我简化了你的代码。我建议避免使用文件系统,因为那样会减慢程序的速度。我也不确定函数的真正目标是什么,所以我只返回HTTP响应。

var req
在这里根本没有被使用,并且accessKey、templateName、outputName在任何地方都没有定义。这是为什么?这是一个错误,变量应该是
data