@elysiajs/cors

中文文档
1.4.3发布于 2026/11/05MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 324万

Elysia plugin to support Cross Origin Requests (CORs)

原始使用文档
文档目录10 节
  1. Installation
  2. Example
  3. Config
  4. origin
  5. methods
  6. allowedHeaders
  7. exposedHeaders
  8. credentials
  9. maxAge
  10. preflight

@elysia/cors

Elysia to support Cross Origin Requests (CORs)

Installation

bun add @elysia/cors

Example

import { Elysia } from 'elysia'
import { cors } from '@elysia/cors'

const app = new Elysia()
    .use(cors())
    .listen(8080)

Config

origin

@default true

Assign the Access-Control-Allow-Origin header.

Value can be one of the following:

  • string - String of origin which will directly assign to Access-Control-Allow-Origin

  • boolean - If set to true, the request origin will be reflected in Access-Control-Allow-Origin

  • RegExp - Pattern to use to test with request's url, will accept origin if matched.

  • Function - Custom logic to validate origin acceptance or not. will accept origin if true is returned.

    • Function will accepts Context just like Handler
    // Example usage
    app.use(cors, {
        origin: ({ request, headers }) => true
    })
    
    // Type Definition
    type CORSOriginFn = (context: Context) => boolean | void
    
  • Array<string | RegExp | Function> - Will try to find truthy value of all options above. Will accept Request if one is true.

methods

@default *

Assign Access-Control-Allow-Methods header.

Value can be one of the following: Accept:

  • undefined | null | '' - Ignore all methods.

  • * - Accept all methods.

  • HTTPMethod - Will be directly set to Access-Control-Allow-Methods.

    • Expects either a single method or a comma-delimited string (eg: 'GET, PUT, POST')
  • HTTPMethod[] - Allow multiple HTTP methods.

    • eg: ['GET', 'PUT', 'POST']

allowedHeaders

@default *

Assign Access-Control-Allow-Headers header.

Allow incoming request with the specified headers.

Value can be one of the following:

  • string

    • Expects either a single method or a comma-delimited string (eg: 'Content-Type, Authorization').
  • string[] - Allow multiple HTTP methods.

    • eg: ['Content-Type', 'Authorization']

exposedHeaders

@default *

Assign Access-Control-Exposed-Headers header.

Return the specified headers to request in CORS mode.

Value can be one of the following:

  • string

    • Expects either a single method or a comma-delimited string (eg: 'Content-Type, 'X-Powered-By').
  • string[] - Allow multiple HTTP methods.

    • eg: ['Content-Type', 'X-Powered-By']

credentials

@default false

Assign Access-Control-Allow-Credentials header.

Allow incoming requests to send credentials header.

  • boolean - Available if set to true.

Credentialed CORS should use an explicit origin allowlist:

cors({
    origin: 'https://app.example.com',
    credentials: true
})

For backward compatibility, cors({ credentials: true }) reflects and trusts any request origin. Do not use this configuration for private APIs.

maxAge

@default 5

Assign Access-Control-Max-Age header.

Allow incoming requests to send credentials header.

  • number - Duration in seconds to indicates how long the results of a preflight request can be cached.

preflight

@default true

Add [OPTIONS] /* handler to handle preflight request which response with HTTP 204 and CORS hints.

  • boolean - Available if set to true.

版本与发布记录

@elysiajs/cors 共发布 49 个版本,首次发布于 2022/12/04;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.3latest2026/11/058 天前
1.4.22026/04/246 个月前
1.4.12025/12/2810 个月前
1.4.02025/09/131.2 年前
1.3.32025/05/151.5 年前
1.3.22025/05/151.5 年前

其他发布通道:next → 1.0.0-beta.2,rc → 1.2.0-rc.0,experimental → 1.3.0-exp.0。

关于 @elysiajs/cors 的常见问题

@elysiajs/cors 是什么?

Elysia plugin to support Cross Origin Requests (CORs)

如何安装 @elysiajs/cors?

在项目目录运行 npm install @elysiajs/cors,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:elysia(>= 1.4.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@elysiajs/cors 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elysiajs/cors 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@elysiajs/cors 可以商用吗?

@elysiajs/cors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elysiajs/cors 的下载量有多少?

根据 npm 官方下载统计,@elysiajs/cors 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,240,406 次,日均约 108,014 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elysiajs/cors 还在维护吗?

最新版本 1.4.3 发布于 2026/11/05(8 天前);至今共发布 49 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/12/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。