Skip to main content

技术原理

¥Technical Principles

Fastify 框架及其官方插件中的每一个决定都遵循以下技术原则:

¥Every decision in the Fastify framework and its official plugins is guided by the following technical principles:

  1. 生产中“零”开销

    ¥“Zero” overhead in production

  2. “良好”的开发者体验

    ¥“Good” developer experience

  3. 非常适合小型和大型项目

    ¥Works great for small & big projects alike

  4. 易于迁移到微服务(甚至无服务器)并返回

    ¥Easy to migrate to microservices (or even serverless) and back

  5. 安全和数据验证

    ¥Security & data validation

  6. 如果某个东西可以是插件,那么它很可能应该是

    ¥If something could be a plugin, it likely should be

  7. 易于测试

    ¥Easily testable

  8. 不要给核心打猴子补丁

    ¥Do not monkeypatch core

  9. 语义版本控制和长期支持

    ¥Semantic versioning & Long Term Support

  10. 遵守规范

    ¥Specification adherence

"零" 生产间接费用

¥"Zero" Overhead in Production

Fastify 旨在通过尽可能减少应用的开销来实现其功能。这通常是通过实现快速算法和数据结构以及 JavaScript 特定功能来实现的。

¥Fastify aims to implement its features by adding as minimal overhead to your application as possible. This is usually delivered by implementing fast algorithms and data structures, as well as JavaScript-specific features.

鉴于 JavaScript 不提供零开销的数据结构,这一原则与提供良好的开发者体验和提供更多功能相矛盾,因为通常这些会花费一些开销。

¥Given that JavaScript does not offer zero-overhead data structures, this principle is at odds with providing a great developer experience and providing more features, as usually those cost some overhead.

"良好" 开发者体验

¥"Good" Developer Experience

Fastify 旨在在其运行的性能点上提供最佳的开发者体验。它提供了出色的开箱即用体验,足够灵活,可以适应各种情况。

¥Fastify aims to provide the best developer experience at the performance point it is operating. It provides a great out-of-the-box experience that is flexible enough to be adapted to a variety of situations.

例如,这意味着二进制插件被禁止,因为大多数 JavaScript 开发者无法访问编译器。

¥As an example, this means that binary addons are forbidden because most JavaScript developers would not have access to a compiler.

非常适合小型和大型项目

¥Works great for small and big projects alike

我们认识到,大多数应用一开始都很小,随着时间的推移变得越来越复杂。Fastify 旨在随着应用的复杂性而增长,提供高级功能来构建代码库。

¥We recognize that most applications start small and become more complex over time. Fastify aims to grow with the complexity of your application, providing advanced features to structure your codebase.

易于迁移到微服务(甚至无服务器)并返回

¥Easy to migrate to microservices (or even serverless) and back

如何部署路由并不重要。框架应该 "正常工作"。

¥How you deploy your routes should not matter. The framework should "just work".

安全和数据验证

¥Security and Data Validation

你的 Web 框架是与不可信数据的第一个接触点,它需要充当系统的第一道防线。

¥Your web framework is the first point of contact with untrusted data, and it needs to act as the first line of defense for your system.

如果某个东西可以是一个插件,它可能应该

¥If something could be a plugin, it likely should

我们认识到 Node.js 的 HTTP 框架有无数的用例。在单个模块中迎合它们将使代码库难以维护。因此,我们提供了钩子和选项,允许你根据需要自定义框架。

¥We recognize that there are an infinite amount of use cases for an HTTP framework for Node.js. Catering to them in a single module would make the codebase unmaintainable. Therefore we provide hooks and options to allow you to customize the framework as you please.

易于测试

¥Easily testable

测试 Fastify 应用应该是首要关注的问题。

¥Testing Fastify applications should be a first-class concern.

不要给核心打猴子补丁

¥Do not monkeypatch core

Monkeypatch Node.js API 或安装改变运行时行为的全局变量会使构建模块化应用变得更加困难,并限制 Fastify 的用例。其他框架可以做到这一点,而我们没有。

¥Monkeypatch Node.js APIs or installing globals that alter the behavior of the runtime makes building modular applications harder, and limit the use cases of Fastify. Other frameworks do this and we do not.

语义版本控制和长期支持

¥Semantic Versioning and Long Term Support

我们提供明确的长期支持策略,以便开发者知道何时升级。

¥We provide a clear Long Term Support strategy so developers can know when to upgrade.

遵守规范

¥Specification adherence

带着疑问,我们选择了相关规范定义的严格行为。

¥In doubt, we chose the strict behavior as defined by the relevant Specifications.