问题
First, I write lambda function like:
function jsonResponse(status, header, body) {
return {
headers: Object.assign(header, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json;charset=utf-8'
}),
statusCode: status,
body: JSON.stringify(body)
}
}
export const hello = async (event, context, cb) => {
const rep = {
message: 'v1.0',
event: event
};
cb(null, jsonResponse(201, {}, rep));
return;
};
It work OK, but according the serverless forum the serverless office forum and the blog The code should be
function createResponse(status, header, body) {
return {
headers: Object.assign(header, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json;charset=utf-8'
}),
statusCode: status,
body: JSON.stringify(body)
}
}
export const hello = async (event, context) => {
const rep = {
message: 'v1.0',
event: event
};
return createResponse(201, {}, rep);
};
For using the callback parameter as it’s not officially part of the async handler syntax.
I use the same version nodejs 8.10 as the article, But When I apply the return function, I get 502 error:
{
"message": "Internal server error"
}
My serverless handler configuration:
functions:
first:
handler: handlers/first.hello
events:
- http:
path: first
method: get
When I try to run it locally and it just keep running without any output for this handler.
Can you tell me why it is internal error and how to fix it?
来源:https://stackoverflow.com/questions/52425229/javascript-serverless-error-of-json-object-with-async-return