koa-cors
raw JSON → 0.0.16 verified Sat Apr 25 auth: no javascript deprecated
CORS middleware for Koa (v0.0.16). Inspired by node-cors, this middleware adds Cross-Origin Resource Sharing (CORS) headers to Koa responses. It supports all standard CORS options: origin, expose, maxAge, credentials, methods, and headers. It is a simple, lightweight solution for enabling CORS in Koa apps, but note that the package is old and the Koa ecosystem has evolved significantly since v1. Unmaintained since 2016; consider using @koa/cors for modern Koa 2.x.
Common errors
error TypeError: app.use() requires a generator function ↓
cause Koa v2 uses async functions, but koa-cors expects a generator (Koa v1).
fix
Use @koa/cors or wrap with koa-convert: const convert = require('koa-convert'); app.use(convert(cors()));
Warnings
gotcha koa-cors is designed for Koa v1 (generator-based middleware). Koa v2 uses async/await and may have compatibility issues. ↓
fix Use @koa/cors instead, which supports Koa 2.x.
gotcha Package unmaintained since 2016. No updates for compatibility with modern Node.js. ↓
fix Migrate to active alternatives like @koa/cors or koa2-cors.
Install
npm install koa-cors yarn add koa-cors pnpm add koa-cors Imports
- default
const cors = require('koa-cors');
Quickstart
const Koa = require('koa');
const cors = require('koa-cors');
const app = new Koa();
app.use(cors({ origin: true }));
app.use(async ctx => { ctx.body = 'Hello World'; });
app.listen(3000);