npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@remix-run/cors-middleware

v0.1.5

Published

Middleware for handling CORS in Fetch API servers

Downloads

8,283

Readme

cors-middleware

CORS middleware for Remix. It adds standard CORS response headers to Fetch API servers and can either short-circuit preflight requests or pass them through to app-defined OPTIONS handlers.

Features

  • Preflight Handling - Automatically handles OPTIONS preflight requests
  • Flexible Origin Rules - Supports static, regex, list, and function-based origin policies
  • Credential Support - Supports credentialed requests with spec-safe origin reflection
  • Header Controls - Configure allowed and exposed headers, preflight methods, and max age
  • Private Network Support - Optionally allow private network preflight requests

Installation

npm i remix

Usage

import { createRouter } from 'remix/router'
import { cors } from 'remix/middleware/cors'

let router = createRouter({
  middleware: [
    cors({
      origin: ['https://app.example.com', 'https://admin.example.com'],
      credentials: true,
      exposedHeaders: ['X-Request-Id'],
    }),
  ],
})

router.get('/api/projects', () => {
  return Response.json([{ id: 'p1', name: 'Remix' }], {
    headers: {
      'X-Request-Id': 'req_123',
    },
  })
})

Origin Policies

origin supports:

  • '*' to allow all origins
  • string for a single exact origin
  • RegExp for pattern-based matching
  • Array<string | RegExp> for multiple exact and pattern matches
  • true to reflect the request origin
  • (origin, context) => boolean | string for dynamic policies

Restrict Origins

let router = createRouter({
  middleware: [
    cors({
      origin: ['https://app.example.com', 'https://admin.example.com'],
      credentials: true,
    }),
  ],
})

Dynamic Origin Policies

let router = createRouter({
  middleware: [
    cors({
      origin(origin, context) {
        if (context.url.pathname.startsWith('/public/')) {
          return '*'
        }

        return origin.endsWith('.trusted.example')
      },
    }),
  ],
})

Preflight Behavior

By default, preflight requests are short-circuited with status 204.

let router = createRouter({
  middleware: [
    cors({
      methods: ['GET', 'POST', 'PATCH'],
      allowedHeaders: ['Authorization', 'Content-Type'],
      maxAge: 600,
    }),
  ],
})

Use a function-based allowedHeaders policy when the header allowlist depends on the request:

let router = createRouter({
  middleware: [
    cors({
      allowedHeaders(request) {
        let requestedHeaders = request.headers.get('Access-Control-Request-Headers')

        if (requestedHeaders?.includes('x-admin-token')) {
          return ['Authorization', 'Content-Type', 'X-Admin-Token']
        }

        return ['Authorization', 'Content-Type']
      },
    }),
  ],
})

Function-based allowedHeaders responses vary on Access-Control-Request-Headers, so caches do not reuse a preflight response for a different requested-header set.

Set preflightContinue: true to let downstream handlers process preflight requests. Use preflightStatusCode when you want short-circuited preflight responses to return a status other than 204.

Private Network Preflights

let router = createRouter({
  middleware: [
    cors({
      allowPrivateNetwork: true,
    }),
  ],
})

When allowPrivateNetwork is enabled, the middleware adds Access-Control-Allow-Private-Network: true for preflight requests that ask for private network access.

Expose Response Headers

let router = createRouter({
  middleware: [
    cors({
      exposedHeaders: ['X-Request-Id', 'X-Trace-Id'],
    }),
  ],
})

Caveats

  • CORS is primarily a browser enforcement mechanism. Disallowed non-preflight requests still reach your handlers unless you add separate request validation.
  • When credentials: true is used with origin: '*', the middleware reflects the request origin and adds Vary: Origin so the response stays cache-safe.
  • When allowedHeaders is a function, preflight responses vary on Access-Control-Request-Headers so caches do not reuse a response for a different requested-header set.
  • preflightContinue and preflightStatusCode only affect how preflight OPTIONS requests are handled. They do not change actual request authorization.

Related Packages

  • cop-middleware - Browser-origin protection middleware for unsafe cross-origin requests
  • fetch-router - Router for the web Fetch API
  • headers - Typed HTTP header utilities

Related Work

License

See LICENSE