Framework Compatibility
- cors:
cors
is framework-agnostic but is most commonly used with Express.js. It can be integrated into any Node.js server application. - @fastify/cors:
@fastify/cors
is specifically designed for the Fastify framework, leveraging its plugin architecture for optimal performance and integration. - @koa/cors:
@koa/cors
is built for the Koa framework, providing a middleware solution that aligns with Koa's async/await style. - koa-cors:
koa-cors
is tailored for Koa but is designed for older versions (Koa 1.x). It is less flexible and maintained compared to@koa/cors
.
Performance
- cors:
cors
is efficient for most applications, but its performance can vary depending on how it is configured and used within the Express.js middleware stack. - @fastify/cors:
@fastify/cors
is optimized for performance, making it one of the fastest CORS middleware available, which is crucial for high-performance applications. - @koa/cors:
@koa/cors
offers good performance for Koa applications, but it may not be as fast as@fastify/cors
due to the overhead of middleware execution in Koa. - koa-cors:
koa-cors
is lightweight and introduces minimal overhead, but it lacks the optimizations found in more modern CORS middleware.
Configuration Flexibility
- cors:
cors
is highly configurable, supporting a wide range of options such as allowed origins, methods, headers, and credentials, making it suitable for complex CORS scenarios. - @fastify/cors:
@fastify/cors
provides flexible configuration options, allowing developers to set allowed origins, methods, headers, and more, all while maintaining a simple API. - @koa/cors:
@koa/cors
allows for configurable CORS settings, including support for dynamic origin functions, making it versatile for various use cases. - koa-cors:
koa-cors
offers basic configuration options but is limited compared to the more feature-rich@koa/cors
andcors
packages.
Ease of Use: Code Examples
- cors:
Express CORS Example
const express = require('express'); const cors = require('cors'); const app = express(); app.use(cors({ origin: 'https://example.com', // Allow only this origin methods: ['GET', 'POST'], // Allow only these methods })); app.get('/', (req, res) => { res.send('CORS enabled for example.com'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
- @fastify/cors:
Fastify CORS Example
const fastify = require('fastify')(); const cors = require('@fastify/cors'); fastify.register(cors, { origin: 'https://example.com', // Allow only this origin methods: ['GET', 'POST'], // Allow only these methods }); fastify.get('/', (req, reply) => { reply.send('CORS enabled for example.com'); }); fastify.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
- @koa/cors:
Koa CORS Example
const Koa = require('koa'); const cors = require('@koa/cors'); const app = new Koa(); app.use(cors({ origin: 'https://example.com', // Allow only this origin allowMethods: 'GET, POST', // Allow only these methods })); app.use(ctx => { ctx.body = 'CORS enabled for example.com'; }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
- koa-cors:
Koa CORS Example (Old)
const Koa = require('koa'); const cors = require('koa-cors'); const app = new Koa(); app.use(cors({ origin: 'https://example.com', // Allow only this origin methods: 'GET, POST', // Allow only these methods })); app.use(ctx => { ctx.body = 'CORS enabled for example.com'; }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });