Tudo funciona localmente, mas em prod não funciona, eu faço o deploy, verifico no serverless console e ta tudo ok, mas quando faço a chamada da erro 500 serverless.yml
org: ferreirasavio
app: aws-node-http-api-project
service: alura-serverless
frameworkVersion: '3'
params:
default:
dbName: alura-serverless
dev:
connectionString: mongodb://localhost:27017/${self:params.default.dbName}
prod:
provider:
name: aws
runtime: nodejs14.x
region: eu-west-1
environment:
MONGODB_CONNECTIONSTRING: ${param:connectionString}
MONGODB_DB_NAME: ${param:dbName}
functions:
sendResponse:
handler: api/handlers.sendResponse
events:
- httpApi:
path: /api/results
method: post
# request:
# schemas:
# application/json: ${file(./schemas/sendResponse.json)}
getResult:
handler: api/handlers.getResult
events:
- httpApi:
path: /api/results/{id}
method: get
plugins:
- serverless-offline
esse aqui é meu handlers
const { MongoClient, ObjectId } = require("mongodb");
async function connectToDatabase(){
const client = new MongoClient(process.env.MONGODB_CONNECTIONSTRING)
const connection = await client.connect()
return connection.db(process.env.MONGODB_DB_NAME)
}
function extractBody(event) {
if (!event?.body) {
return {
statusCode: 422,
body: JSON.stringify({ error: "Missing body" }),
};
}
return JSON.parse(event.body);
}
module.exports.sendResponse = async (event) => {
const { name, answers } = extractBody(event);
const correctQuestions = [3, 1, 0, 2];
const totalCorrectAnswers = answers.reduce((acc, answer, index) => {
if (answer === correctQuestions[index]) {
acc++;
}
return acc;
}, 0);
const result = {
name,
answers,
totalCorrectAnswers,
totalAnswers: answers.length,
};
const client = await connectToDatabase()
const collection = await client.collection('results')
const { insertedId} = await collection.insertOne(result)
return {
statusCode: 201,
body: JSON.stringify({
resultId: insertedId,
__hypermedia: {
href: `/results.html`,
query: { id: insertedId },
},
}),
headers: {
'Content-Type': 'application/json'
}
};
};
module.exports.getResult = async (event) => {
const client = await connectToDatabase()
const collection = await client.collection('results')
const result = await collection.findOne({
_id: new ObjectId(event.pathParameters.id)
})
if (!result) {
return {
statusCode: 404,
body: JSON.stringify({error: 'Result not found'}),
headers: {
'Content-Type': 'application/json'
}
}
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(result)
}
}