Registry / http-networking / corser

corser

JSON →
library2.0.1jsnpmunverified

Corser is a highly configurable middleware for Node.js designed to handle Cross-Origin Resource Sharing (CORS). It offers a flexible approach to managing CORS preflight requests and setting appropriate response headers, supporting both static whitelists for allowed origins and dynamic origin checking through a callback function. The package's current stable version is 2.0.1, released in August 2016. Due to the lack of updates since then, its release cadence is effectively non-existent, indicating an abandoned or legacy status despite an 'active' project badge from its active development period. Its key differentiators at the time included robust compatibility with Connect and Express middleware, as well as plain Node.js `http` servers, providing granular control over CORS policies for various server setups. Developers should be aware of its CommonJS-only nature and lack of ongoing maintenance.

npm install corser
INSTALL
IMPORT
SIG · CORSER
C
corser
http-networkingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

corser
const corser = require('corser');
import corser from 'corser'; import { create } from 'corser';
Corser is a CommonJS-only package. It does not support ES Modules directly without transpilation or a CommonJS loader. The main export is the 'corser' object which contains the 'create' method.
corser.create
const corser = require('corser'); const corserMiddleware = corser.create();
import { create } from 'corser'; const corserMiddleware = create();
The primary API is the 'create' method on the default exported 'corser' object, which returns a middleware function.

This example demonstrates how to integrate Corser as middleware within an Express.js application, allowing all cross-origin requests by default.

const express = require('express'); const corser = require('corser'); const app = express(); // Configure Corser to allow all origins by default. // For production, specify a whitelist: corser.create({ origins: ['http://localhost:3000'] }) app.use(corser.create()); app.get('/', function (req, res) { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Nice weather today, huh?'); }); app.listen(1337, () => { console.log('Server listening on http://localhost:1337'); }); // To run this example: // 1. npm install express corser // 2. node your_script.js
Debug
Known issues
breakingIn version 2.0.0, the default behavior for preflight requests changed: they are now automatically closed. To retain previous behavior (where you handle OPTIONS requests yourself), you must explicitly set `endPreflightRequests` to `false` in the configuration object.
fix
If you need to handle `OPTIONS` requests manually, configure Corser with `corser.create({ endPreflightRequests: false })` and implement your own handler.
affects: >=2.0.0
breakingThe callback function for dynamic origin checking changed its signature in v2.0.0 from `(matches)` to `(err, matches)`. Existing implementations relying on the old signature will break.
fix
Update your dynamic origin callback function to accept an error parameter: `function(origin, callback) { callback(null, matches); }`.
affects: >=2.0.0
gotchaCorser is a CommonJS-only package. Attempting to import it using ES Module `import` syntax will result in errors in modern Node.js environments unless a compatible transpiler or loader is used.
fix
Always use `const corser = require('corser');` for importing. If in an ESM project, consider using a different, actively maintained CORS middleware that supports ESM.
affects: >=0.4.0
gotchaThe package has not been updated since August 2016. This means it lacks support for newer Node.js features, potential security updates, or bug fixes for modern browser behaviors beyond what was addressed in v2.0.1 for Chrome 52.
fix
Evaluate newer, actively maintained CORS middleware solutions like the 'cors' package (npmjs.com/package/cors) for modern applications.
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: corser.create is not a function
Attempting to use `import corser from 'corser';` or `import * as corser from 'corser';` which incorrectly handles the CommonJS default export, or trying to destructure a non-existent named export.
fix
Use the correct CommonJS `require` syntax: `const corser = require('corser');` then `corser.create()`.
ReferenceError: require is not defined
Attempting to use `require('corser')` within an ES Module (type: 'module' in package.json or .mjs file) context in Node.js.
fix
Either convert your project to CommonJS (remove 'type: module' from package.json) or use a different CORS middleware that supports ES Modules. You could also explore `createRequire` for advanced interoperability, but it's generally not recommended for simple imports.
Access to fetch at '...' from origin '...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
The `origins` configuration in `corser.create()` is either too restrictive, the requested origin is not whitelisted, or the dynamic origin function is failing to match.
fix
Ensure the `origins` array explicitly includes the client's origin (e.g., `['http://localhost:3000']`) or that your dynamic origin function correctly returns `true` for the callback. Check for typos in the origin URL. Also ensure Corser middleware is applied correctly and not skipped.
The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
CORS middleware is being applied multiple times in the request processing chain, leading to duplicate 'Access-Control-Allow-Origin' headers.
fix
Review your middleware setup to ensure `app.use(corser.create())` or similar is called only once per request or once during app initialization. This often happens if it's included in a generic router that's also mounted globally.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
expressoptionalCommon integration for web server middleware, used in examples.
connectoptionalCommon integration for web server middleware, used in examples.
Agent activity
29 hits · last 30 days
node
26
OpenAI (training)
1
Resources
corser — npm install corser · libregistry