Skip to content

Commit 8619476

Browse files
committed
Allow custom cors headers to be supplied via serverless.yml
1 parent ebd07c7 commit 8619476

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ custom:
108108

109109
You can find an example setups in the [`examples`](./examples) folder.
110110

111+
### Custom CORS headers when serving locally
112+
You can add custom allowed CORS headers using the `custom` section in the `serverless.yml`.
113+
These will get appended to the normal set of headers (`Authorization`, `Content-Type`, `x-amz-date`, `x-amz-security-token`)
114+
and sent when using the local `serverless webpack serve` command.
115+
116+
```yaml
117+
# serverless.yml
118+
custom:
119+
allowedCorsHeaders:
120+
- X-My-Auth-Token
121+
- X-My-Api-Host
122+
```
123+
111124
## Usage
112125

113126
### Automatic bundling

lib/serve.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,15 @@ module.exports = {
9393
},
9494

9595
_handlerAddCors(handler) {
96+
const customHeaders = (
97+
this.serverless.service.custom &&
98+
this.serverless.service.custom.allowedCorsHeaders
99+
) || [];
100+
const allowedCorsHeaders = ['Authorization,Content-Type,x-amz-date,x-amz-security-token'].concat(customHeaders).join(',');
96101
return (req, res, next) => {
97102
res.header('Access-Control-Allow-Origin', '*');
98103
res.header('Access-Control-Allow-Methods', 'GET,PUT,HEAD,PATCH,POST,DELETE,OPTIONS');
99-
res.header('Access-Control-Allow-Headers', 'Authorization,Content-Type,x-amz-date,x-amz-security-token');
104+
res.header('Access-Control-Allow-Headers', allowedCorsHeaders);
100105
handler(req, res, next);
101106
};
102107
},

0 commit comments

Comments
 (0)