dherault / dherault/serverless-offline

CORS headers missing when invoking function via aws sdk

Open
#1,692 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
5.3k
Forks
811
Avg merge
2d 4h
Merged PRs (30d)
3

Description

## Bug Report

No CORS headers (e.g. Access-Control-Allow-Origin and Access-Control-Allow-Credentials) are present when invoking a function via aws sdk. Deployed lambda works fine.

**Current Behavior**
Error in console
`Access to XMLHttpRequest at 'http://localhost:4001/2015-03-31/functions/serverless-action-exports-dev-exportTable/invocations' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.`

**Sample Code**

- file: serverless.yml

```yaml
provider:
name: aws
runtime: nodejs14.x
stage: dev
region: us-east-1
memorySize: 10240

functions:
exportTable:
handler: src/index.exportTable
timeout: 900

plugins:
- serverless-offline

custom:
serverless-offline:
lambdaPort: 4001
```

**Expected behavior/code**

CORS headers are present and no error in console.

**Environment**

- `serverless` version: [e.g. v3.30.1]
- `serverless-offline` version: [e.g. v12.0.4]
- `node.js` version: [e.g. v14.21.3]
- `OS`: 13.3.1

**Possible Solution**

Looks like need to handle CORS here https://github.com/dherault/serverless-offline/blob/v12.0.4/src/lambda/HttpServer.js same as here https://github.com/dherault/serverless-offline/blob/v12.0.4/src/events/http/HttpServer.js#L107
Adding onPreResponse fixes the issue for me ![](https://i.gyazo.com/0aea0c0fe0d7f042246384d40cecd29d.png)

**Additional context/Screenshots**

![](https://i.gyazo.com/3a431d101399d1b3fd3d7ff6f07fb61a.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.