Cross-Origin Resource Sharing,跨域资源共享
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS
https://developer.mozilla.org/zh-CN/docs/Glossary/CORS
image.png

egg-cors

https://github.com/eggjs/egg-cors

  1. npm i egg-cors --save

koa-cors

https://github.com/koajs/cors

  1. npm install @koa/cors --save
  1. /**
  2. * CORS middleware
  3. * @param {Object} [options]
  4. * - {String|Function(ctx)} origin `Access-Control-Allow-Origin`, default is request Origin header
  5. * - {String|Array} allowMethods `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'
  6. * - {String|Array} exposeHeaders `Access-Control-Expose-Headers`
  7. * - {String|Array} allowHeaders `Access-Control-Allow-Headers`
  8. * - {String|Number} maxAge `Access-Control-Max-Age` in seconds
  9. * - {Boolean|Function(ctx)} credentials `Access-Control-Allow-Credentials`, default is false.
  10. * - {Boolean} keepHeadersOnError Add set headers to `err.header` if an error is thrown
  11. * - {Boolean} secureContext `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` headers.', default is false
  12. * - {Boolean} privateNetworkAccess handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`, default to false
  13. * @return {Function} cors middleware
  14. * @api public
  15. */