Skip to content

CORS 插件

¥CORS Plugin

此插件增加了自定义 跨域资源共享 行为的支持。

¥This plugin adds support for customizing Cross-Origin Resource Sharing behavior.

使用以下工具安装:

¥Install with:

bash
bun add @elysiajs/cors

然后使用它:

¥Then use it:

typescript
import { 
Elysia
} from 'elysia'
import {
cors
} from '@elysiajs/cors'
new
Elysia
().
use
(
cors
()).
listen
(3000)

这将设置 Elysia 接受来自任何来源的请求。

¥This will set Elysia to accept requests from any origin.

配置

¥Config

以下是插件接受的配​​置。

¥Below is a config which is accepted by the plugin

origin

@default true

指示响应是否可以与来自给定来源的请求代码共享。

¥Indicates whether the response can be shared with the requesting code from the given origins.

值可以是以下之一:

¥Value can be one of the following:

  • string - 将直接分配给 Access-Control-Allow-Origin 标头的来源名称。

  • boolean - 如果设置为 true,Access-Control-Allow-Origin 将设置为 *(任何来源)

  • RegExp - 匹配请求 URL 的模式,如果匹配则允许。

  • 函数 - 允许资源共享的自定义逻辑,如果返回 true,则允许共享。

    • 预期类型:
    typescript
    cors(context: Context) => boolean | void
  • Array<string | RegExp |函数> - 按顺序遍历上述所有情况,如果任何值是 true,则允许。


methods

@default *

允许跨域请求的方法。

¥Allowed methods for cross-origin requests.

分配 Access-Control-Allow-Methods 标头。

¥Assign Access-Control-Allow-Methods header.

值可以是以下之一:

¥Value can be one of the following:

  • 未定义 | 空 | '' - 忽略所有方法。

      • 允许所有方法。
  • string - 预期为单个方法或逗号分隔的字符串

    • (例如:'GET, PUT, POST'
  • string[] - 允许多个 HTTP 方法。

    • 例如:['GET', 'PUT', 'POST']

allowedHeaders

@default *

允许传入请求的标头。

¥Allowed headers for an incoming request.

分配 Access-Control-Allow-Headers 标头。

¥Assign Access-Control-Allow-Headers header.

值可以是以下之一:

¥Value can be one of the following:

  • string - 预期为单个标头或逗号分隔的字符串

    • 例如:'Content-Type, Authorization'.
  • string[] - 允许多个 HTTP 标头。

    • 例如:['Content-Type', 'Authorization']

exposeHeaders

@default *

响应指定标头的 CORS。

¥Response CORS with specified headers.

分配 Access-Control-Expose-Headers 标头。

¥Assign Access-Control-Expose-Headers header.

值可以是以下之一:

¥Value can be one of the following:

  • string - 预期为单个标头或逗号分隔的字符串。

    • 例如:'Content-Type, X-Powered-By'.
  • string[] - 允许多个 HTTP 标头。

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

credentials

@default true

Access-Control-Allow-Credentials 响应头告知浏览器,当请求的凭据模式 Request.credentialsinclude 时,是否将响应暴露给前端 JavaScript 代码。

¥The Access-Control-Allow-Credentials response header tells browsers whether to expose the response to the frontend JavaScript code when the request's credentials mode Request.credentials is include.

当请求的凭证模式 Request.credentialsinclude 时,仅当 Access-Control-Allow-Credentials 值为 true 时,浏览器才会将响应公开给前端 JavaScript 代码。

¥When a request's credentials mode Request.credentials is include, browsers will only expose the response to the frontend JavaScript code if the Access-Control-Allow-Credentials value is true.

凭证可以是 Cookie、授权标头或 TLS 客户端证书。

¥Credentials are cookies, authorization headers, or TLS client certificates.

分配 Access-Control-Allow-Credentials 标头。

¥Assign Access-Control-Allow-Credentials header.


maxAge

@default 5

指示 预检请求 的结果(即 Access-Control-Allow-MethodsAccess-Control-Allow-Headers 标头中包含的信息)可以缓存多长时间。

¥Indicates how long the results of a preflight request (that is the information contained in the Access-Control-Allow-Methods and Access-Control-Allow-Headers headers) can be cached.

分配 Access-Control-Max-Age 标头。

¥Assign Access-Control-Max-Age header.


preflight

预检请求是发送的请求,用于检查 CORS 协议是否被理解,以及服务器是否知道使用特定的方法和标头。

¥The preflight request is a request sent to check if the CORS protocol is understood and if a server is aware of using specific methods and headers.

响应带有 3 个 HTTP 请求标头的 OPTIONS 请求:

¥Response with OPTIONS request with 3 HTTP request headers:

  • 访问控制请求方法

  • 访问控制请求标头

  • 来源

此配置指示服务器是否应响应预检请求。

¥This config indicates if the server should respond to preflight requests.

模式

¥Pattern

以下是使用该插件的常见模式。

¥Below you can find the common patterns to use the plugin.

允许通过顶层域名进行 CORS

¥Allow CORS by top-level domain

typescript
import { 
Elysia
} from 'elysia'
import {
cors
} from '@elysiajs/cors'
const
app
= new
Elysia
()
.
use
(
cors
({
origin
: /.*\.saltyaom\.com$/
}) ) .
get
('/', () => 'Hi')
.
listen
(3000)

这将允许使用 saltyaom.com 从顶层域名发出请求。

¥This will allow requests from top-level domains with saltyaom.com