The cors package sets Access-Control-* headers for Express and Connect apps so browsers can read cross-origin responses. It supports static or dynamic origin functions and preflight OPTIONS handling.
Overview
cors is Node.js middleware that configures Cross-Origin Resource Sharing headers on HTTP responses. It does not block requests; browsers decide whether client-side JavaScript may read answers based on the returned headers.
Simple usage enables all origins, while configuration objects control methods, headers, credentials, and max-age values. Dynamic origin callbacks allow loading allowed hosts from databases or environment-specific rules.
Preflight OPTIONS handling integrates with route-level or app-wide middleware so non-simple verbs and custom headers work in browsers. Documentation clarifies common misconceptions that CORS is not authentication or server-side access control.
Installation uses npm, and the module remains widely used across Express APIs that must be reachable from browser frontends.
Key features
- Express Connect middleware
- Configurable allow lists
- Dynamic origin callbacks
- Preflight OPTIONS support
- Credential and header controls
Use cases
- Browser APIs on separate domains
- Local frontend to remote API
- Per-route CORS policies
- Credential-sharing SPAs