nextjs-cors

Drop-in CORS middleware for Next.js API routes, built on the battle-tested cors package

Library
npm
v2.2.1
224stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
40/100Fair
Development Activity52
Maintenance0
Community28
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
57/100Fair
Architecture50
Code Quality48
Innovation40
Learning Curve88

nextjs-cors adapts the widely-used Express cors middleware for Next.js API routes, which don’t natively expose the (req, res, next) middleware signature Express plugins expect. It wraps cors in a small Promise-based function you can await at the top of any API route handler, giving you the same configuration options (allowed origins, methods, headers, credentials) documented by the underlying cors package.

The implementation is intentionally minimal — a single TypeScript file that bridges Next.js’s request/response objects into the callback-style API the cors package expects — making it easy to audit and unlikely to introduce its own bugs beyond whatever cors itself does.

What You Get

  • A single async NextCors(req, res, options) function usable in any Next.js API route handler
  • Full pass-through of the cors package’s configuration options (origin, methods, allowed/exposed headers, credentials, preflight status code)
  • TypeScript types for Next.js’s NextApiRequest/NextApiResponse built into the function signature
  • Zero custom CORS logic — behavior matches the well-known cors package exactly, reducing surprises
  • Small bundle footprint since it ships as a thin wrapper with cors as its only runtime dependency

Common Use Cases

  • Enabling a Next.js API route to be called cross-origin from a separate frontend deployment or mobile app
  • Allowing a public API built with Next.js API routes to be consumed by third-party developers from the browser
  • Restricting which origins, methods, or headers are permitted on a set of internal Next.js API endpoints
  • Migrating an existing Express app that used cors middleware to Next.js API routes without rewriting CORS config

Under The Hood

Architecture: The entire package is one file, src/index.ts, which wraps the cors package’s callback-based middleware in a Promise: it invokes cors(options)(req, res, callback) and resolves or rejects based on whether the callback receives an Error, letting Next.js API route handlers await NextCors(...) instead of dealing with the (req, res, next) middleware signature Express expects.

Tech Stack: Built with TypeScript and bundled via tsup (producing both CJS and ESM outputs plus .d.ts types), with cors as its sole runtime dependency and next declared as a peer dependency (>=8.1.1-canary.54) so it works across a wide range of Next.js versions; releases are managed with Changesets.

Code Quality: The codebase is a single ~25-line function with no branching logic beyond the Promise/error bridge, so correctness depends almost entirely on the well-established cors package it wraps; there is no dedicated test suite in the repository, meaning correctness is verified indirectly through cors’s own test coverage and real-world usage rather than tests in this repo itself.

API Design: The API mirrors Express usage patterns Next.js developers migrating from Express are likely to already know, and requires no configuration objects of its own — any options object valid for the cors package works unchanged, so there’s effectively zero additional learning curve on top of already knowing cors.

Used by 5 apps in this directory

TypeScript
76%
AGPL 3.0

Abby

Developer Tools · Product Management

167

Statically typed feature flags, remote config, and A/B testing with framework-native SDKs for TypeScript teams.

View details
30
Repo Health
70
Technical
63
Dependency
Built with
TypeScript76%
MDX18%
Updated 1 years ago
TypeScript
89%
Other

FastGPT

AI Agents · AI Development

29,387

Build, debug, and deploy knowledge-based AI agents with a visual workflow editor, RAG retrieval, and support for any OpenAI-compatible LLM.

View details
93
Repo Health
84
Technical
70
Dependency
Built with
TypeScript89%
Updated today
TypeScript
71%
Other

highlight.io

Developer Tools · Analytics · Monitoring

9,367

Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.

View details
69
Repo Health
78
Technical
66
Dependency
Built with
TypeScript71%
Go16%
Updated yesterday
TypeScript
98%
AGPL 3.0

Kan

Project Management

5,357

An open-source, self-hostable Kanban board built as a modern Trello alternative with team workspaces, board permissions, and Trello import.

View details
85
Repo Health
78
Technical
67
Dependency
Built with
TypeScript98%
Updated 1 weeks ago
TypeScript
63%
Other

Typebot

Developer Tools · AI Development · No Code Platforms

10,268

Build sophisticated chatbots visually, embed them anywhere without iframes, and own your data — fully self-hostable with a modular block system and 30+ integrations.

View details
94
Repo Health
82
Technical
67
Dependency
Built with
TypeScript63%
MDX36%
Updated today

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search