cors
Express/Node.js middleware for handling Cross-Origin Resource Sharing.
3. Used by agents' projects
Dependency in 196 public GitHub projects set up for AI coding agents that we scanned (3.1%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What cors does
cors is a small Express/Connect middleware that only sets CORS response headers, and its README states plainly that this neither blocks requests nor protects an API.
cors popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How cors is used
- cors is a dependency in 196 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (4.0% of the 4,867 JavaScript/TypeScript ones), #4 of 102 in http servers and apis.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 816; those are left out of every share on this page.
- The most used alternative in our scan is NestJS (35 projects).
- Projects that use it most often also use Express (98.0%), dotenv (73.5%), jsonwebtoken (40.8%).
- It is more common in projects set up for Devin Desktop (10.8%) than for OpenAI Codex (1.9%).
By coding agent
Often used with
cors in practice
Strengths
<code>origin</code> can be an array that mixes strings and regular expressions. The README's example accepts one exact origin plus any subdomain of a second domain.
The function form of <code>origin</code> receives the request origin and a <code>callback(error, origin)</code>. The README says this form is meant for loading allowed origins from storage.
You can pass a function of <code>(req, callback)</code> instead of static options. The README's example gives <code>/auth/connect/</code> routes one origin with <code>credentials: true</code> and opens every other route with <code>*</code>.
With <code>app.use(cors())</code>, OPTIONS preflight requests are handled for every route without extra handlers.
Watch out for
With no options, the middleware uses origin <code>*</code> and allows GET, HEAD, PUT, PATCH, POST and DELETE. To narrow this, set <code>origin</code> yourself.
The README warns that curl, Postman and other servers ignore CORS. It points to authentication and authorization for protecting an API.
If you attach <code>cors()</code> 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 <code>app.options('/path', cors())</code>, or across the board with <code>app.options('*', cors())</code> placed before other routes.
Allow a single origin on one route
var express = require('express')
var cors = require('cors')
var app = express()
var corsOptions = {
origin: 'http://example.com',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
// Adds headers: Access-Control-Allow-Origin: http://example.com, Vary: Origin
app.get('/products/:id', cors(corsOptions), function (req, res, next) {
res.json({msg: 'Hello'})
})
app.listen(80, function () {
console.log('web server listening on port 80')
})More about cors
Funding
- Open Collective: https://opencollective.com/express
Packages around it
- express: web framework it plugs into
- dotenv: env var loading
- jsonwebtoken: JWT sign and verify
- tsx: run TypeScript directly
- helmet: security headers
- nodemon: auto-restart in dev
More in HTTP servers and APIs
Alternatives
cors This page
Progressive Node.js framework for building scalable and maintainable server-side applications.
Get started
Install it.
npm install corsRules for your agent
## 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.