You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # egg-cors
  2. [![NPM version][npm-image]][npm-url]
  3. [![build status][travis-image]][travis-url]
  4. [![Test coverage][codecov-image]][codecov-url]
  5. [![David deps][david-image]][david-url]
  6. [![Known Vulnerabilities][snyk-image]][snyk-url]
  7. [![npm download][download-image]][download-url]
  8. [npm-image]: https://img.shields.io/npm/v/egg-cors.svg?style=flat-square
  9. [npm-url]: https://npmjs.org/package/egg-cors
  10. [travis-image]: https://img.shields.io/travis/eggjs/egg-cors.svg?style=flat-square
  11. [travis-url]: https://travis-ci.org/eggjs/egg-cors
  12. [codecov-image]: https://codecov.io/github/eggjs/egg-cors/coverage.svg?branch=master
  13. [codecov-url]: https://codecov.io/github/eggjs/egg-cors?branch=master
  14. [david-image]: https://img.shields.io/david/eggjs/egg-cors.svg?style=flat-square
  15. [david-url]: https://david-dm.org/eggjs/egg-cors
  16. [snyk-image]: https://snyk.io/test/npm/egg-cors/badge.svg?style=flat-square
  17. [snyk-url]: https://snyk.io/test/npm/egg-cors
  18. [download-image]: https://img.shields.io/npm/dm/egg-cors.svg?style=flat-square
  19. [download-url]: https://npmjs.org/package/egg-cors
  20. [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) plugin for egg, based on [@koa/cors](https://github.com/koajs/cors).
  21. ## Install
  22. ```bash
  23. $ npm i egg-cors --save
  24. ```
  25. ## Usage
  26. ```js
  27. // {app_root}/config/plugin.js
  28. exports.cors = {
  29. enable: true,
  30. package: 'egg-cors',
  31. };
  32. ```
  33. `egg-cors` works internally with [egg-security](https://github.com/eggjs/egg-security). By defining the property of `domainWhiteList` on object `security`, you have successfully informed the framework to whitelist the passed domains.
  34. When you make a request from client side, **egg** should return an `Access-Control-Allow-Origin` response header with the domain that you passed in along with the payload and status code *200*.
  35. ```js
  36. exports.security = {
  37. domainWhiteList: [ 'http://localhost:4200' ],
  38. };
  39. ```
  40. ## Configuration
  41. Support all configurations in [@koa/cors](https://github.com/koajs/cors).
  42. ```js
  43. // {app_root}/config/config.default.js
  44. exports.cors = {
  45. // {string|Function} origin: '*',
  46. // {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  47. };
  48. ```
  49. If the `origin` is set, the plugin will follow it to set the `Access-Control-Allow-Origin` and ignore the `security.domainWhiteList`. Otherwise, the `security.domainWhiteList` which is default will take effect as described above.
  50. ## Security
  51. Only in safe domain list support CORS when security plugin enabled.
  52. ## Questions & Suggestions
  53. Please open an issue [here](https://github.com/eggjs/egg/issues).
  54. ## License
  55. [MIT](LICENSE)