Simple CORS Middleware for Micro

0.1.1 · maintenance · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up `micro-cors` with a basic `micro` handler, including options for allowed methods and explicit handling for CORS preflight requests.

import { send } from 'micro';
import microCors from 'micro-cors';

const cors = microCors({
  allowMethods: ['GET', 'POST', 'OPTIONS'],
  allowHeaders: ['X-Requested-With', 'Access-Control-Allow-Origin', 'X-HTTP-Method-Override', 'Content-Type', 'Authorization', 'Accept'],
  origin: '*'
});

const handler = async (req, res) => {
  if (req.method === 'OPTIONS') {
    // Handle preflight requests explicitly if needed
    return send(res, 200, 'ok!');
  }
  
  if (req.method === 'POST') {
    // Example POST request handling
    const data = await json(req);
    return send(res, 200, { message: 'Received POST data', data });
  }

  // Default GET response
  return send(res, 200, { message: 'Hello from micro-cors!' });
};

export default cors(handler);

view raw JSON →