AWS LAMBDA - NodeJS POST请求和异步读/写文件 [英] AWS Lambda - NodeJS POST request and asynch write/read file
问题描述
我是新来的NodeJS和内部AWS拉姆达我试图让调用一个JSON对象的外部API POST请求,创建一个具有响应的文件,然后读取该文件的内容。
来自一个Ruby的背景下,我想从我的不熟悉异步编程问题的根源,但我已经使用回调尝试和readfileSync只是没有运气调试。
任何帮助将是AP preciated。
VAR的查询字符串=需要('查询字符串');
VAR HTTPS =需要('HTTPS');
变种FS =要求(FS);
exports.handler =函数(事件上下文){
的console.log('接收到的事件:,JSON.stringify(事件,空,2));
变种运行= event.operation;
删除event.operation;
VAR ACCESSKEY = event.accessKey;
删除event.accessKey;
VAR TEMPLATENAME = event.templateName;
删除event.templateName;
VAR outputName = event.outputName;
删除event.outputName;
VAR REQ = {
ACCESSKEY:快速键,
TEMPLATENAME:TEMPLATENAME,
outputName:outputName,
数据:event.data
};
函数的doPost(数据,回调){
//生成字符串后从对象
变种post_data = JSON.stringify(数据);
//选项的对象,以指示张贴到
VAR post_options = {
主持人:hostname.com,
端口:'443',
路径:/路径/要/ API,
方法:POST,
标题:{
内容类型:应用/ JSON的,
内容长度:post_data.length
}
};
//设置请求
var文件= fs.createWriteStream(outputName);
VAR post_req = https.request(post_options,功能(RES){
res.setEncoding('UTF8');
res.pipe(文件);
res.on('回应',函数(响应){
的console.log(响应);
});
res.on('错误',功能(五){
context.fail(错误:+ e.message);
})
res.on(结束,函数(){
context.succeed('成功,结束请求监听器);
});
});
//发布数据
post_req.write(post_data);
post_req.end();
回电话();
}
功能printFileContents(){
fs.readFileSync(outputName,'UTF8',功能(ERR,数据){
的console.log('文件的内容:+数据);
});
}
开关(操作){
案创造:
//确保有数据之前,我们将它张贴
如果(REQ){
doPost方法(REQ,printFileContents);
printFileContents();
}
打破;
...
}
};
在一般情况下,我建议你开始是这样的:
VAR的查询字符串=需要('查询字符串');
VAR HTTPS =需要('HTTPS');
变种FS =要求(FS);
exports.handler =函数(事件上下文){
console.info(接收到的事件,事件);
VAR REQ = {
ACCESSKEY:快速键,
TEMPLATENAME:TEMPLATENAME,
outputName:outputName,
数据:event.data
};
//生成字符串后从对象
变种post_data = JSON.stringify(数据);
//选项的对象,以指示张贴到
VAR post_options = {
主持人:hostname.com,
端口:'443',
路径:/路径/要/ API,
方法:POST,
标题:{
内容类型:应用/ JSON的,
内容长度:post_data.length
}
};
VAR post_request = https.request(post_options,功能(RES){
VAR体='';
res.on('数据',函数(块){
机身+ =块;
});
res.on(结束,函数(){
context.done(体);
});
res.on('错误',功能(五){
context.fail(错误:+ e.message);
});
});
//发布数据
post_request.write(post_data);
post_request.end();
};
您可以看到我简化你的code颇有几分。我建议你避免了文件系统,因为这会减慢你的程序。我也不能确定什么是你的函数的真正目标,所以我只返回HTTP响应。
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.
这篇关于AWS LAMBDA - NodeJS POST请求和异步读/写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!