Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
@koa/cors is a middleware for Koa.js that enables Cross-Origin Resource Sharing (CORS) with various configuration options. It allows you to specify which origins are permitted to access your resources, as well as other CORS-related settings.
Basic CORS Setup
This code sets up a basic Koa server with CORS enabled for all origins. It uses the @koa/cors middleware without any additional configuration.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors());
app.listen(3000);
Restricting Origins
This code configures the @koa/cors middleware to only allow requests from 'https://example.com'.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors({ origin: 'https://example.com' }));
app.listen(3000);
Configuring Additional CORS Options
This code demonstrates how to configure additional CORS options such as allowed methods, headers, exposed headers, max age, and credentials.
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors({
origin: '*',
allowMethods: ['GET', 'POST'],
allowHeaders: ['Content-Type', 'Authorization'],
exposeHeaders: ['Content-Length', 'Date'],
maxAge: 3600,
credentials: true
}));
app.listen(3000);
The 'cors' package is a popular middleware for enabling CORS in Express.js applications. It offers similar functionality to @koa/cors but is designed for use with Express.js instead of Koa.js.
The 'fastify-cors' package is a CORS plugin for Fastify, a fast and low-overhead web framework for Node.js. It provides similar CORS configuration options as @koa/cors but is tailored for Fastify applications.
The 'hapi-cors' package is a CORS plugin for Hapi.js, a rich framework for building applications and services. It offers similar CORS functionality to @koa/cors but is designed for use with Hapi.js.
Cross-Origin Resource Sharing(CORS) for koa
$ npm install @koa/cors --save
Enable cors with default options:
const Koa = require('koa');
const cors = require('@koa/cors');
const app = new Koa();
app.use(cors());
/**
* 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
* @return {Function} cors middleware
* @api public
*/
FAQs
Cross-Origin Resource Sharing(CORS) for koa
The npm package @koa/cors receives a total of 417,344 weekly downloads. As such, @koa/cors popularity was classified as popular.
We found that @koa/cors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.