serverless-express: [Question] Internal server error when request method OPTIONS

Had anyone same issue? When I try via postman make a request method OPTIONS the response is {"message": "Internal server error"} if I try in API Gateway Console, lambda Console, npm run local and npm run invoke-lambda the response is ok. Any idea what is the problem?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 26 (3 by maintainers)

Commits related to this issue

Most upvoted comments

With the help from staff on AWS forums, I’ve resolved it by adding:

x-amazon-apigateway-integration:
  contentHandling: CONVERT_TO_TEXT
  ...

which now returns a correct 200 response for the OPTIONS request. It wasn’t obvious we needed to do that however, especially that this option is not available for Mock requests through the Console yet.

@samikarak that needs to be added to the configuration of your OPTIONS methods in the Swagger template (https://github.com/awslabs/aws-serverless-express/blob/master/example/simple-proxy-api.yaml#L43)

If you’re not using swagger, which I wasn’t, a couple things solved the issue for me. First was enabling CORS in the serverless.yml

functions:
  api:
    handler: lambda.handler
    events:
      - http:
          path: /
          method: ANY
          cors: true
      - http:
          path: /{proxy+}
          method: ANY
          cors: true

The second was making sure my Express setup allowed the OPTIONS method through:

app.use( (req, res, next) => {
   res.header("Access-Control-Allow-Origin", "*")
   res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")
   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
   next();
 })

Hope this helps some people out. It took me a bit to figure out all the pieces.

@brettstack would you explain this" remove the OPTIONS method and redeploy your API and let Express deal with CORS."

Thanks @Jun711. This has been merged into develop branch and we’ll merge into master soon. Closing.

Okay I’ll leave this open until I can confirm. Alternative is to simply remove the OPTIONS method from the Swagger template and let Lamdba handle the preflight request. Having the OPTIONS method is a performance enhancement (i.e. the response will be faster since it’s terminated at the API Gateway layer)

I’ll verify this later. For now, simply remove the OPTIONS method and redeploy your API and let Express deal with CORS.