我正在查看的教程正在使用 Node 6.1 for AWS Lambda 函数
export.fn = (event,context,callback) {
callback(null,event)
};
我想使用 Node.12。
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from G!'),
};
return response;
}
如何重写我的函数以使其可以接受 JSON 数据?
{
"persondata": {
"name": "Max",
"length": 29
}
}
响应正文
{
"statusCode": 200,
"body": "\"Hello from G!\""
}
日志
Mon Dec 16 11:38:57 UTC 2019 : HTTP Method: POST, Resource Path: /compare-yourself
Mon Dec 16 11:38:57 UTC 2019 : Method request path: {}
Mon Dec 16 11:38:57 UTC 2019 : Method request query string: {}
Mon Dec 16 11:38:57 UTC 2019 : Method request headers: {}
Mon Dec 16 11:38:57 UTC 2019 : Method request body before transformations: {
"persondata" : {
"name" : "Max",
"length" : 29
}
}
最佳答案
您可以从 lambda 返回 json,如下所示:
exports.handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify({
"persondata": {
"name": "Max",
"length": 29
}
})
};
return response;
}
关于node.js - 如何使用 Node 12 为 AWS Lambda 函数创建事件响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59355560/