Skip to content

与 Expo 集成

¥Integration with Expo

从 Expo SDK 50 和 App Router v3 开始,Expo 允许我们直接在 Expo 应用中创建 API 路由。

¥Starting from Expo SDK 50, and App Router v3, Expo allows us to create API route directly in an Expo app.

  1. 如果 Expo 应用不存在,则创建一个:
typescript
bun create expo-app --template tabs
  1. 创建 app/[...slugs]+api.ts
  2. 在 [...slugs]+api.ts 中,创建或导入现有的 Elysia 服务器
  3. 使用你想要公开的方法名称导出处理程序
typescript
// app/[...slugs]+api.ts
import { Elysia, t } from 'elysia'

const app = new Elysia()
    .get('/', () => 'hello Next')
    .post('/', ({ body }) => body, {
        body: t.Object({
            name: t.String()
        })
    })

export const GET = app.handle 
export const POST = app.handle 

由于符合 WinterCG 标准,Elysia 将按预期正常工作,但是,如果你在 Node 上运行 Expo,某些插件(例如 Elysia Static)可能无法正常工作。

¥Elysia will work normally as expected because of WinterCG compliance, however, some plugins like Elysia Static may not work if you are running Expo on Node.

你可以将 Elysia 服务器视为普通的 Expo API 路由。

¥You can treat the Elysia server as if normal Expo API route.

通过这种方法,你可以将前端和后端共存于一个存储库中,并且 使用 Eden 实现端到端类型安全 可以同时执行客户端和服务器操作。

¥With this approach, you can have co-location of both frontend and backend in a single repository and have End-to-end type safety with Eden with both client-side and server action.

更多信息请参阅 API 路由

¥Please refer to API route for more information.

前缀

¥Prefix

如果你将 Elysia 服务器放置在应用路由的根目录之外,则需要将前缀注释为 Elysia 服务器。

¥If you place an Elysia server not in the root directory of the app router, you need to annotate the prefix to the Elysia server.

例如,如果你将 Elysia 服务器放在 app/api/[...slugs]+api.ts 中,则需要将 Elysia 服务器的前缀注释为 /api。

¥For example, if you place Elysia server in app/api/[...slugs]+api.ts, you need to annotate prefix as /api to Elysia server.

typescript
// app/api/[...slugs]+api.ts
import { Elysia, t } from 'elysia'

const app = new Elysia({ prefix: '/api' })
    .get('/', () => 'hi')
    .post('/', ({ body }) => body, {
        body: t.Object({
            name: t.String()
        })
    })

export const GET = app.handle
export const POST = app.handle

这将确保 Elysia 路由在你放置它的任何位置都能正常工作。

¥This will ensure that Elysia routing will work properly in any location you place it in.

部署

¥Deployment

你可以直接使用 Elysia 的 API 路由,并在需要时像普通的 Elysia 应用一样部署,或者使用 实验性 Expo 服务器运行时

¥You can either directly use API route using Elysia and deploy as normal Elysia app normally if need or using experimental Expo server runtime.

如果你使用 Expo 服务器运行时,则可以使用 expo export 命令为你的 expo 应用创建优化构建,这将包含一个使用 Elysia 的 Expo 函数,路径为 dist/server/_expo/functions/[...slugs]+api.js。

¥If you are using Expo server runtime, you may use expo export command to create optimized build for your expo app, this will include an Expo function which is using Elysia at dist/server/_expo/functions/[...slugs]+api.js

提示

请注意,Expo 函数被视为 Edge 函数而非普通服务器,因此直接运行 Edge 函数不会分配任何端口。

¥Please note that Expo Functions are treated as Edge functions instead of normal server, so running the Edge function directly will not allocate any port.

你可以使用 Expo 提供的 Expo 函数适配器来部署你的 Edge Function。

¥You may use the Expo function adapter provided by Expo to deploy your Edge Function.

目前 Expo 支持以下适配器:

¥Currently Expo support the following adapter: