@koa/cors ======= [![NPM version][npm-image]][npm-url] [![Node.js CI](https://github.com/koajs/cors/actions/workflows/nodejs.yml/badge.svg)](https://github.com/koajs/cors/actions/workflows/nodejs.yml) [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url] [npm-image]: https://img.shields.io/npm/v/@koa/cors.svg?style=flat-square [npm-url]: https://npmjs.org/package/@koa/cors [codecov-image]: https://codecov.io/github/koajs/cors/coverage.svg?branch=v2.x [codecov-url]: https://codecov.io/github/koajs/cors?branch=v2.x [download-image]: https://img.shields.io/npm/dm/@koa/cors.svg?style=flat-square [download-url]: https://npmjs.org/package/@koa/cors [Cross-Origin Resource Sharing(CORS)](https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS) for koa ## Installation ```bash $ npm install @koa/cors --save ``` ## Quick start Enable cors with default options: - origin: request Origin header - allowMethods: GET,HEAD,PUT,POST,DELETE,PATCH ```js const Koa = require('koa'); const cors = require('@koa/cors'); const app = new Koa(); app.use(cors()); ``` ## cors(options) ```js /** * CORS middleware * * @param {Object} [options] * - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header * - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH' * - {String|Array} exposeHeaders `Access-Control-Expose-Headers` * - {String|Array} allowHeaders `Access-Control-Allow-Headers` * - {String|Number} maxAge `Access-Control-Max-Age` in seconds * - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`, default is false. * - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown * - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false * - {Boolean} privateNetworkAccess handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`, default to false * @return {Function} cors middleware * @api public */ ``` ## License [MIT](./LICENSE) ## Contributors |[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
omsmith](https://github.com/omsmith)
|[
jonathanong](https://github.com/jonathanong)
|[
AlphaWong](https://github.com/AlphaWong)
|[
cma-skedulo](https://github.com/cma-skedulo)
| | :---: | :---: | :---: | :---: | :---: | :---: | |[
erikfried](https://github.com/erikfried)
|[
j-waaang](https://github.com/j-waaang)
|[
ltomes](https://github.com/ltomes)
|[
lfreneda](https://github.com/lfreneda)
|[
matthewmueller](https://github.com/matthewmueller)
|[
PlasmaPower](https://github.com/PlasmaPower)
| [
swain](https://github.com/swain)
|[
TyrealHu](https://github.com/TyrealHu)
|[
xg-wang](https://github.com/xg-wang)
|[
lishengzxc](https://github.com/lishengzxc)
|[
mcohen75](https://github.com/mcohen75)
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Aug 19 2022 23:48:53 GMT+0800`.