gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:41 24 4
gpt4 key购买 nike

我是 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 响应。

关于node.js - AWS Lambda - NodeJS POST 请求和异步写入/读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com