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

@lytjs/middleware-cors

v6.9.6

Published

LytJS CORS Middleware - Cross-Origin Resource Sharing middleware

Readme

@lytjs/middleware-cors

LytJS CORS 中间件。

npm version license

简介

@lytjs/middleware-cors 是 LytJS 框架的 CORS 中间件,用于处理跨域资源共享。

核心特性

  • 灵活的源配置:支持字符串、数组、正则表达式
  • 自动预检处理:自动响应 OPTIONS 预检请求
  • 自定义头部:可自定义允许和暴露的头部
  • 凭证支持:支持携带凭证的跨域请求
  • 零依赖:不引入任何外部依赖

安装

npm install @lytjs/middleware-cors

或使用 pnpm:

pnpm add @lytjs/middleware-cors

快速开始

基本用法

import { createMiddlewareChain } from '@lytjs/middleware';
import { createCorsMiddleware } from '@lytjs/middleware-cors';

const chain = createMiddlewareChain();

// 允许所有源
chain.use(createCorsMiddleware());

// 或者只允许特定源
chain.use(
  createCorsMiddleware({
    origin: 'https://example.com',
  }),
);

配置 CORS

import { createCorsMiddleware } from '@lytjs/middleware-cors';

// 允许多个源
const cors = createCorsMiddleware({
  origin: ['https://example.com', 'https://app.example.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization'],
  exposedHeaders: ['X-Custom-Header'],
  credentials: true,
  maxAge: 86400, // 24 hours
});

使用正则表达式

const cors = createCorsMiddleware({
  origin: /\.example\.com$/,
  credentials: true,
});

使用函数自定义

const cors = createCorsMiddleware({
  origin: (requestOrigin) => {
    const allowedOrigins = ['https://a.com', 'https://b.com'];
    if (requestOrigin && allowedOrigins.includes(requestOrigin)) {
      return requestOrigin;
    }
    return false;
  },
});

主要 API

createCorsMiddleware(options)

创建 CORS 中间件。

import { createCorsMiddleware } from '@lytjs/middleware-cors';

const cors = createCorsMiddleware({
  origin: '*',
  methods: ['GET', 'POST'],
  credentials: false,
  maxAge: 86400,
});

选项说明

| 选项 | 类型 | 默认值 | 说明 | | ---------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | ---------------------- | | origin | string | string[] | RegExp | boolean | (origin: string | null) => boolean | string | '*' | 允许的来源 | | methods | string | string[] | ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'] | 允许的 HTTP 方法 | | allowedHeaders | string | string[] | ['Accept', 'Accept-Language', 'Content-Language', 'Content-Type'] | 允许的请求头部 | | exposedHeaders | string | string[] | | 暴露的响应头部 | | credentials | boolean | false | 是否允许携带凭证 | | maxAge | number | | 预检请求缓存时间(秒) |

许可证

MIT License - 查看许可证

贡献指南

欢迎提交 Issue 和 Pull Request!