# Feathers（框架）

**Feathers**（FeathersJS）是一个轻量级的 Node.js 框架，它围绕统一的服务接口而非路由和控制器来构建 Web API 和实时应用。服务是一个对象或类，实现 `find`、`get`、`create`、`update`、`patch` 和 `remove` 等标准方法；一旦注册，同一个服务会自动通过 REST、websocket 和内部调用暴露出来，还可以在浏览器、[React Native](https://www.edgepedia.org/zh/react-native) 和 Node.js 的客户端侧运行。<sup>[1](https://feathersjs.com/api/services)</sup><sup> • </sup><sup>[2](https://feathersjs.com/api/client)</sup> 该项目始于 2013 年，于 2014 年发布首个稳定版本，并被从初创公司到财富 500 强企业的各类公司用于生产环境。<sup>[3](https://feathersjs.com/help/faq)</sup><sup> • </sup><sup>[4](https://feathersjs.com/guides/whats-new)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心抽象 | 服务：实现标准 CRUD 方法以及自定义 `(data, params)` 方法的对象或类；每个注册的服务都会成为一个 NodeJS EventEmitter<sup>[1](https://feathersjs.com/api/services)</sup> |
| 中间件 | 钩子：以环绕方式或在服务方法执行之前、之后或出错时注册的可插拔函数；与传输方式无关，且与服务无关<sup>[5](https://feathersjs.com/api/hooks)</sup> |
| 传输方式 | HTTP 使用 Express 和 Koa，实时事件使用 Socket.io；相同的服务 API 在所有传输方式下均可工作<sup>[6](https://feathersjs.com/guides/cli/app)</sup><sup> • </sup><sup>[7](https://feathersjs.com/api/channels)</sup>  |
| 当前版本 | v5（Dove），一次完整的 TypeScript 重写，包含类型化客户端、默认 KoaJS 传输和基数树路由器；最新版本 v5.0.50（2026-09-11）<sup>[4](https://feathersjs.com/guides/whats-new)</sup><sup> • </sup><sup>[8](https://github.com/feathersjs/feathers/releases)</sup>  |
| 认证 | `@feathersjs/authentication` 提供用户名/密码、JWT 和 OAuth（GitHub、Facebook 等）策略，以及一个 `authenticate` 钩子<sup>[9](https://feathersjs.com/api/authentication/)</sup> |
| 数据库支持 | 针对 MongoDB、SQL（通过 Knex）和内存存储的适配器实现了用于初始化、分页和查询的通用接口；任何数据库都可以通过自定义服务来使用<sup>[10](https://feathersjs.com/api/databases/common)</sup><sup> • </sup><sup>[3](https://feathersjs.com/help/faq)</sup>  |
| 历史 | 始于 2013 年，2014 年发布首个稳定版本；v6 于 2024 年 6 月宣布，意在回归现代 Web 标准上的基本原则<sup>[3](https://feathersjs.com/help/faq)</sup><sup> • </sup><sup>[11](https://github.com/feathersjs/feathers/discussions/3498)</sup>  |

## 服务

服务是每个 Feathers 应用的核心。服务是一个对象或类的实例，实现了特定方法，为 Feathers 提供了一种统一的、与协议无关的方式来与数据源交互：数据库、文件系统，或第三方 API，如用于电子邮件的 MailGun、用于支付的 Stripe 或用于天气数据的 OpenWeatherMap。<sup>[1](https://feathersjs.com/api/services)</sup><sup> • </sup><sup>[12](https://feathersjs.com/guides/basics/services)</sup> 服务不必连接到数据库；自定义服务可以实现任何功能，例如与另一个 API 通信或发送电子邮件。<sup>[1](https://feathersjs.com/api/services)</sup>

标准服务方法接收 `id`（资源标识符）、`data`（资源数据）和 `params`（附加调用参数），并且必须是 `async` 或返回 Promise。一旦通过 `app.use()` 注册，服务就可以通过 `app.service('my-service')` 获取和调用。<sup>[1](https://feathersjs.com/api/services)</sup> 成功的 `create` 调用会发出一个带有返回数据的 `created` 事件，当结果为数组时，每个条目发出一个事件。<sup>[1](https://feathersjs.com/api/services)</sup>

**自定义方法。** 除标准方法外，服务还可以暴露签名为 `(data, params)` 的自定义方法。所有需要对外暴露的方法，包括标准方法，都必须在注册服务时列在 `methods` 选项中；该选项仅适用于通过 HTTP 或 websocket 的外部访问，而所有方法在服务器内部仍然可用。<sup>[1](https://feathersjs.com/api/services)</sup>

## 钩子

钩子是以环绕方式或在服务方法执行之前、之后或出错时注册的可插拔中间件函数，多个钩子可以串联成工作流。钩子与传输方式无关，因此无论调用是来自服务器内部、通过 HTTP(S) 的 REST 还是通过 websocket 到达，其行为都相同；它也与服务无关，可与任何服务配合使用，无论该服务是否使用数据库。<sup>[5](https://feathersjs.com/api/hooks)</sup>

钩子通常用于权限、验证、日志记录、认证、数据模式和解析器以及发送通知。钩子 `context` 对象携带有关调用的信息，包括只读属性 `context.app`（应用对象）、`context.service`、`context.path` 和 `context.method`，以及可写属性如 `context.data` 和 `context.result`。在前置钩子中设置 `context.result` 会跳过原始服务方法调用，而其他钩子仍会运行。<sup>[5](https://feathersjs.com/api/hooks)</sup> 通过 `app.hooks()` 注册的应用级钩子会为每个服务运行，特殊的 `setup` 和 `teardown` 钩子在应用启动和关闭时各运行一次，例如用于打开和关闭 MongoDB 连接。<sup>[5](https://feathersjs.com/api/hooks)</sup>

## 实时事件与通道

所有 Feathers 事件都通过 NodeJS EventEmitter 接口提供，配置 Socket.io 后，事件会自动从服务器推送到已连接的客户端。<sup>[13](https://feathersjs.com/api/events)</sup> 事件通道决定哪些已连接的客户端接收实时事件以及发送的数据是什么样子。通道是一个由连接对象组成的命名数组；通道中的任何连接都会接收发送到该通道的所有事件。典型用途包括：仅向已认证用户发送事件、将更新限制在用户已加入的聊天室，或向非管理员发送仅包含 `email`、`id` 和 `avatar` 等字段的“安全”版本用户对象。<sup>[7](https://feathersjs.com/api/channels)</sup> 发布函数按服务或应用注册，接收事件数据和钩子上下文，并返回要投递到的一个或多个通道。通道管理仅存在于服务器端；仅提供 REST 的 API 没有通道。<sup>[7](https://feathersjs.com/api/channels)</sup>

## 认证

`@feathersjs/authentication` 插件提供了用户名/密码、JWT 和 OAuth 认证工具，支持 GitHub 和 Facebook 等提供商，以及自定义机制。核心模块包括用于注册策略和管理访问令牌的 `AuthenticationService`、`JWTStrategy`，以及将服务调用限制为给定策略的 `authenticate` 钩子。该模块是认证机制的抽象，不处理用户验证或密码重置，这些可以通过 feathers-authentication-management 等库或 Auth0 等平台添加。<sup>[9](https://feathersjs.com/api/authentication/)</sup> `authenticate` 钩子根据策略名称列表检查 `params.authentication`，对外部调用失败时抛出错误，成功时将策略结果合并到 `params` 中。<sup>[14](https://feathersjs.com/api/authentication/hook)</sup>

## 数据库适配器

每个 Feathers 数据库适配器都是 Feathers 服务接口的实现，适配器共享用于初始化、分页、扩展和查询的通用 API。每个适配器导出一个 `<Name>Service` 类，使用 `id`、`events` 和 `paginate` 等选项实例化。<sup>[10](https://feathersjs.com/api/databases/common)</sup> 官方适配器包括 MongoDB、通过 Knex 的 SQL 以及内存存储；社区维护的适配器涵盖其他数据库，任何数据库仍然可以通过编写自定义服务来使用。Feathers 团队目前没有直接添加新数据库支持的计划，但社区适配器可以升级为官方适配器。<sup>[3](https://feathersjs.com/help/faq)</sup>

## 客户端

Feathers 既可以在服务器上运行，也可以在客户端上运行，而且它不是单独的库：客户端获得相同的功能，包括服务、钩子和插件。客户端创建的服务通过 REST 或 websocket 连接中继方法调用，并且使用 Socket.io 等实时传输方式时可以监听服务器事件。相关模块包括用于应用的 `@feathersjs/feathers`、`@feathersjs/rest-client`、`@feathersjs/socketio-client` 和 `@feathersjs/authentication-client`；捆绑的 `@feathersjs/client` 模块可以通过 `<script>` 标签直接在浏览器中加载。在客户端使用 Feathers 是可选的，因为 REST 和 Socket.io 连接可以直接使用。<sup>[2](https://feathersjs.com/api/client)</sup> 使用 Feathers v5 或更高版本时，CLI 会生成一个客户端文件，导出服务器端模式中定义的类型，因此类型只需定义一次即可在两端使用。<sup>[2](https://feathersjs.com/api/client)</sup><sup> • </sup><sup>[4](https://feathersjs.com/guides/whats-new)</sup>

## 版本 5（Dove）与通往 v6 之路

Feathers v5，代号 Dove，用 [TypeScript](https://www.edgepedia.org/zh/typescript) 完全重写了所有 Feathers 代码，从核心到数据库适配器、内置钩子和工具，CLI 现在默认生成 TypeScript 应用。它增加了在服务器和客户端之间共享类型的类型化客户端、新的 `@feathersjs/koa` 适配器（成为 CLI 的默认传输方式，与继续存在的 Express 适配器并列）、内置的基数树路由器（[Fastify](https://www.edgepedia.org/zh/fastify) 速度背后的算法，无论底层传输方式如何都能工作），以及用于动态生成应用的服务注销功能。<sup>[4](https://feathersjs.com/guides/whats-new)</sup> 从 v4（Crow）迁移涉及以下更改：将查询 `whitelist` 选项拆分为升级后适配器的 `filters` 和 `operators`、异步的 `app.setup` 和 `app.listen`，以及更新 Socket.io 和 Grant（OAuth）依赖项。<sup>[15](https://feathersjs.com/guides/migrating)</sup>

2024 年 6 月，项目负责人宣布 Feathers v6（没有鸟类代号）将回归以服务、钩子和 HTTP 及实时 API 为核心的基本原则，基于现代 Web 标准，面向 NodeJS、Deno、BunJS 和 [Cloudflare Workers](https://www.edgepedia.org/zh/cloudflare-workers)。认证模块、Express、Koa 和 Socket.io 适配器以及模式集成将以维护模式移至生态系统中，数据库适配器也被重新评估，理由是在普通服务内部直接进行数据库调用现在比通用适配器抽象更容易编写和类型化。<sup>[11](https://github.com/feathersjs/feathers/discussions/3498)</sup>

## 在 Node.js 框架中的定位

Feathers 采取了与 Rails、Sails 或 [NestJS](https://www.edgepedia.org/zh/nestjs) 等传统 MVC 框架以及 Sinatra、Express 或 Fastify 等底层 HTTP 框架都不同的方法：它不使用路由、控制器和请求处理器，而是使用服务和钩子，使应用逻辑独立于访问方式，从而能够从同一份代码提供 REST 和 websocket API，并在不更改应用代码的情况下添加新协议。<sup>[3](https://feathersjs.com/help/faq)</sup> 与 LoopBack（一个基于路由的 CRUD 配置的 MVC 框架）相比，Feathers 推崇面向服务的思维，其中每个实体都是一个带有 CRUD 方法的轻量级服务。<sup>[16](https://feathersjs.com/feathers-vs-loopback)</sup> 一篇比较七个 Node.js 框架（Express、[Hapi](https://www.edgepedia.org/zh/hapi-web-framework)、Loopback、Feathers、Koa、Nest 和 Sails）的硕士论文发现，Nest 在其理论模型上评价最佳，而支持更多功能的框架性能较低。<sup>[17](https://files01.core.ac.uk/download/162457678.pdf)</sup>

## References

1. [Services, Feathers documentation](https://feathersjs.com/api/services)
2. [Feathers Client, Feathers documentation](https://feathersjs.com/api/client)
3. [FAQ, Feathers documentation](https://feathersjs.com/help/faq)
4. [What's New in v5, Feathers documentation](https://feathersjs.com/guides/whats-new)
5. [Hooks, Feathers documentation](https://feathersjs.com/api/hooks)
6. [Application (CLI), Feathers documentation](https://feathersjs.com/guides/cli/app)
7. [Channels, Feathers documentation](https://feathersjs.com/api/channels)
8. [Releases, feathersjs/feathers on GitHub](https://github.com/feathersjs/feathers/releases)
9. [Authentication Overview, Feathers documentation](https://feathersjs.com/api/authentication/)
10. [Common API (database adapters), Feathers documentation](https://feathersjs.com/api/databases/common)
11. [Forward to Feathers v6 - back to the basics, feathersjs/feathers Discussion #3498, GitHub](https://github.com/feathersjs/feathers/discussions/3498)
12. [Services (guides), Feathers documentation](https://feathersjs.com/guides/basics/services)
13. [Events, Feathers documentation](https://feathersjs.com/api/events)
14. [Authenticate Hook, Feathers documentation](https://feathersjs.com/api/authentication/hook)
15. [Migrating to v5, Feathers documentation](https://feathersjs.com/guides/migrating)
16. [Feathers vs LoopBack, Feathers documentation](https://feathersjs.com/feathers-vs-loopback)
17. [Comparison of Node.js frameworks for development of JavaScript web servers, master's thesis, Faculty of Electrical Engineering](https://files01.core.ac.uk/download/162457678.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/feathers-framework

English original: https://www.edgepedia.org/feathers-framework
