UNPKG

1.72 kBMarkdownView Raw
1# koa2-cors
2
3## install
4
5```bash
6npm install --save koa2-cors
7```
8
9## Usage
10
11```js
12var koa = require('koa');
13var cors = require('koa2-cors');
14
15var app = koa();
16app.use(cors());
17```
18
19## Options
20
21### origin
22
23Configures the **Access-Control-Allow-Origin** CORS header. expects a string. Can also be set to a function, which takes the `ctx` as the first parameter.
24
25### exposeHeaders
26
27Configures the **Access-Control-Expose-Headers** CORS header. Expects a comma-delimited array.
28
29### maxAge
30
31Configures the **Access-Control-Max-Age** CORS header. Expects a
32Number.
33
34### credentials
35
36Configures the **Access-Control-Allow-Credentials** CORS header. Expects a Boolean.
37
38### allowMethods
39
40Configures the **Access-Control-Allow-Methods** CORS header. Expects a comma-delimited array , If not specified, default allowMethods is `['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']`.
41
42### allowHeaders
43Configures the **Access-Control-Allow-Headers** CORS header. Expects a comma-delimited array . If not specified, defaults to reflecting the headers specified in the request's **Access-Control-Request-Headers** header.
44
45```js
46var koa = require('koa');
47var cors = require('koa2-cors');
48
49var app = koa();
50app.use(cors({
51 origin: function(ctx) {
52 if (ctx.url === '/test') {
53 return false;
54 }
55 return '*';
56 },
57 exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
58 maxAge: 5,
59 credentials: true,
60 allowMethods: ['GET', 'POST', 'DELETE'],
61 allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
62}));
63...
64```
65
66[More details about CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS).
67
68## License
69
70[MIT License](http://www.opensource.org/licenses/mit-license.php)