Skip to main content
Version: 10.x

适配器

tRPC 本身并不是一个服务器,因此必须使用其他主机提供服务,例如简单的 Node.js HTTP 服务器Express 甚至 Next.js。无论你选择哪个后端,大多数 tRPC 功能都是相同的。适配器充当主机系统和 tRPC API 之间的粘合剂。

¥tRPC is not a server on its own, and must therefore be served using other hosts, such as a simple Node.js HTTP Server, Express, or even Next.js. Most tRPC features are the same no matter which backend you choose. Adapters act as the glue between the host system and your tRPC API.

适配器通常遵循一些通用约定,允许你通过 createContext 设置上下文创建,并通过 onError 全局处理错误,但重要的是允许你为应用选择合适的主机。

¥Adapters typically follow some common conventions, allowing you to set up context creation via createContext, and globally handle errors via onError, but importantly allow you to choose an appropriate host for your application.

我们支持多种托管 API 的模式,你可以在此处找到相关记录。

¥We support many modes of hosting an API, which you will find documented here.

  • 对于 serverful API,你可能需要我们的 独立式 适配器,或者使用 ExpressFastify 适配器来连接到你现有的 API

    ¥For serverful APIs, you might want our Standalone adapter, or use the Express or Fastify adapters to hook into your existing APIs

  • 你可能需要无服务器解决方案并选择 AWS Lambda拿来 作为边缘运行时

    ¥You might want a serverless solution and choose AWS Lambda, or Fetch for edge runtimes

  • 你可能拥有一个全栈框架,并希望像 Next.js 一样完全集成,或者你可以将 拿来 适配器与 Next.js、Astro、Remix 或 SolidStart 结合使用

    ¥You might have a full-stack framework and want a full integration like Next.js, or you could use the Fetch adapter with Next.js, Astro, Remix, or SolidStart

提示

对于本地开发或服务器基础设施,最简单的适配器是 独立适配器,它可用于运行标准 Node.js HTTP 服务器。当你需要快速入门并且没有现有的 HTTP 服务器可集成时,我们建议你这样做。如果你的需求发生变化,以后更换也很简单。

¥For local development or serverful infrastructure, the simplest Adapter to use is the Standalone Adapter, which can be used to run a standard Node.js HTTP Server. We recommend this when you need to get started quickly and have no existing HTTP Server to integrate with. Swapping out later is trivial if your needs change.