# cors — card for AI agents

Express/Node.js middleware for handling Cross-Origin Resource Sharing.

Category: HTTP servers and APIs · npm package `cors` · latest version 2.8.6 (checked 2026-11-01)
Install: `npm install cors`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## cors
- Do not use cors to protect an API. The server still processes every request, so add authentication and authorization separately.
- Bare `cors()` sends `Access-Control-Allow-Origin: *`. When that is not intended, set `origin` to a string, RegExp, array or function.
- For credential-sensitive routes, follow the README's example: it sets `credentials: true` together with one specific `origin`.
- If you attach `cors()` per route instead of `app.use(cors())`, add `app.options('/path', cors())` for routes that use DELETE or custom headers. Alternatively, put `app.options('*', cors())` before other routes.
- In an `origin` function, call `callback(error, origin)`. The `origin` value must be a non-function value that the `origin` option accepts.
- Set `allowedHeaders` and `exposedHeaders` when you need control over them. If they are unset, request headers are reflected and no custom response headers are exposed.
- Set `optionsSuccessStatus: 200` if legacy browsers such as IE11 or SmartTVs must work.

## What it is
- cors is a Node.js middleware for Express and Connect that sets CORS response headers. The README says those headers tell browsers which origins can read responses from your server. You mount `cors()` once with `app.use` for the whole app, or pass it to individual routes.
- Configuration is a single options object. Its fields map to the `Access-Control-*` headers: `origin`, `methods`, `allowedHeaders`, `exposedHeaders`, `credentials` and `maxAge`. When `origin` is a specific value, the README's examples show the middleware sending `Vary: Origin` next to `Access-Control-Allow-Origin`. You can also pass a function in place of the options object to build a policy for each request.
- It is for Express or Connect servers whose responses must be readable by browser JavaScript from another origin. The README frames this carefully: CORS is enforced by browsers, and it is not access control.

## Caveats
- Defaults allow every origin: With no options, the middleware uses origin `*` and allows GET, HEAD, PUT, PATCH, POST and DELETE. To narrow this, set `origin` yourself.
- Not access control: The README warns that curl, Postman and other servers ignore CORS. It points to authentication and authorization for protecting an API.
- Per-route use needs preflight handlers: If you attach `cors()` to single routes, "complex" requests need an OPTIONS handler. Examples are DELETE and requests with custom headers. You can add the handler per route with `app.options('/path', cors())`, or across the board with `app.options('*', cors())` placed before other routes.
- Request headers are reflected by default: If `allowedHeaders` is not set, the middleware echoes back whatever the browser listed in `Access-Control-Request-Headers`.
- Default 204 and legacy browsers: Successful OPTIONS responses default to status 204. The option description says some legacy browsers choke on 204. The README's configuration example sets `optionsSuccessStatus: 200` with that note as a comment.

## Numbers (AgentGid data)
- Gid Score: 76.8 (#163 of all tools, 8 of 102 in its category)
- Used in 196 public projects set up for AI coding agents (3.1%)
- Downloads last week: 130,553,200 (as of 2026-11-24), +46.3% over 4 weeks
- GitHub stars: 6,198 (as of 2026-11-24)
- Last push: 2026-06-02

## Often used with
Express 98.0%, dotenv 73.5%, jsonwebtoken 40.8%, Helmet 37.2%, uuid 31.1%, Zod 56.1%

## Checked alternatives (same job)
- NestJS: https://agentgid.com/tools/nestjs/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/cors/
- Homepage: https://github.com/expressjs/cors#readme
- Repository: https://github.com/expressjs/cors
- Source: https://github.com/expressjs/cors#readme
- Source: https://github.com/expressjs/cors
- Source: https://github.com/expressjs/cors/releases

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
