I am using serverless to invoke a lambda function in the handler.js function, but every time I try to execute the function with my click listener I get a cors error in the console, but I have cors set to true in my .yml file. I pasted my code below:
indent preformatted text by 4 spaces
serverless.yml:
service: aws-nodejs
provider:
name: aws
runtime: nodejs1
region: us-east-1
role: arn:aws:iam::429122815342:role/service-role/GTLambdaRole
functions:
analyze:
handler: handler.analyze
events:
- http:
path: analyze
method: post
cors: true
handler.js:
let AWS = require(“aws-sdk”);
let comprehend = AWS.Comprehend();
module.exports.analyze = (event, context, callback) => {
let body = JSON.parse(event.body);
const params = {
Text: body.text
};
// Detecting the dominant language of the text
comprehend.detectDominantLanguage(params, function (err, result) {
if (!err) {
const language = result.Languages[0].LanguageCode;
const sentimentParams = {
Text: body.text,
LanguageCode: language
};
// Analyze the sentiment
comprehend.detectSentiment(sentimentParams, function (err, data) {
if (err) {
callback(null, {
statusCode: 400,
headers: {
“Access-Control-Allow-Origin”: ‘’,
“Access-Control-Allow-Methods”: “OPTIONS,POST,GET”,
‘Access-Control-Allow-Credentials’: true
},
body: JSON.stringify(err)
});
} else {
callback(null, {
statusCode: 200,
headers: {
“Access-Control-Allow-Origin”: '’,
“Access-Control-Allow-Methods”: “OPTIONS,POST,GET”,
‘Access-Control-Allow-Credentials’: true
},
body: JSON.stringify(data)
});
}
});
}
});
}Preformatted text