The `access-control` package offers a minimal and straightforward implementation for managing HTTP Access Control (CORS) according to the W3C specification. It is designed as a focused utility for applications needing to handle cross-origin requests, abstracting the complexities of CORS header management. As of its last known release, the package is at version 1.0.1, published over 8 years ago, indicating it is no longer actively maintained. Its core functionality involves configuring allowed origins, HTTP methods, credentials handling, preflight request caching (`maxAge`), and exposing/allowing specific headers. A key differentiator is its direct handling of `OPTIONS` preflight requests and automatic `403 Forbidden` responses for invalid CORS attempts, as well as automatic adjustment of `Access-Control-Allow-Origin` when `*` is combined with `credentials: true` for specification compliance.
npm install access-controlVerified import paths — ran on the pinned version, not inferred.
Illustrates how to configure `access-control` with specific origins and credentials, and integrate the resulting middleware into a Node.js HTTP server to handle CORS preflight requests and secure responses.
Migrate to an actively maintained CORS middleware solution like `cors` from npm or implement CORS headers manually.
Be aware of this automatic adjustment. If you require `*` origin and credentials, your setup is non-compliant and this library correctly modifies behavior. Consider specific origins instead of `*` when credentials are needed.
If using ESM, ensure your bundler or Node.js environment is configured to handle CommonJS imports. Consider using a TypeScript-first or ESM-native CORS solution for modern projects.
First call `access(options)` to get the middleware, then use the returned function: `const cors = access({ ... }); http.createServer(function (req, res) { if (cors(req, res)) return; ... });`Ensure the client's `Origin` header exactly matches one of the allowed origins (e.g., `'http://example.com'`) or configure `origins` to `*` if appropriate (though with caution for security). Also, verify `methods` and `headers` options cover all operations the client intends to perform.