micro-cors is a straightforward middleware designed to enable Cross-Origin Resource Sharing (CORS) for applications built with `micro`, a minimalist asynchronous HTTP microservices framework. The package's current stable version is `0.1.1`, which was last published approximately seven years ago. While `1.0.0-alpha` versions were released around five years ago, indicating a past attempt at a major update, development appears to have stalled in alpha, leaving the `0.x` branch as the de facto stable release. It maintains a slow release cadence, essentially being in a maintenance mode. Its key differentiator is its small footprint and specific integration with the `micro` ecosystem, aiming for minimal configuration to handle CORS headers.
npm install micro-corsVerified import paths — ran on the pinned version, not inferred.
Demonstrates setting up `micro-cors` with a basic `micro` handler, including options for allowed methods and explicit handling for CORS preflight requests.
Consider using alternative CORS middleware solutions specifically designed for serverless platforms (e.g., AWS Lambda, Vercel Serverless Functions) if deploying to such environments. For containerized `micro` applications, ensure proper container networking and CORS configuration.
Evaluate the stability and long-term support risks before adopting. For new projects, consider more actively maintained CORS solutions or frameworks that offer built-in CORS handling.
If `allowCredentials` is `true`, `origin` must be set to a specific domain or a function that dynamically determines the allowed origin based on the request's `Origin` header.
Explicitly check `req.method === 'OPTIONS'` within your `micro` handler and send a 200 OK response with the appropriate CORS headers before proceeding to your main logic. The quickstart example demonstrates this.
Ensure `module.exports = cors(handler)` is used and that `cors` is the result of `microCors()` or `microCors(options)`.
Install the community-maintained type definitions: `npm install --save-dev @types/micro-cors` or `yarn add -D @types/micro-cors`.
Verify the `origin` option in `micro-cors` is set to match the client's origin (e.g., `origin: 'http://localhost:3000'`) or `origin: '*'` for public APIs (but be mindful of security implications). For local development, `origin: '*'` is often used. Ensure preflight `OPTIONS` requests are handled correctly and send the necessary `Access-Control-Allow-Origin` header.