Fast and low overhead web framework, for Node.js
| Files | Last commit | Last update |
|---|---|---|
| 16 days ago | ||
| 26 days ago | ||
| 16 days ago | ||
| 7 months ago | ||
| 2 years ago | ||
| 13 days ago | ||
| 2 months ago | ||
| 13 days ago | ||
| 25 days ago | ||
| 1 year ago | ||
| 6 years ago | ||
| 1 year ago | ||
| 7 months ago | ||
| 2 years ago | ||
| 2 years ago | ||
| 7 months ago | ||
| 1 month ago | ||
| 6 years ago | ||
| 2 years ago | ||
| 4 months ago | ||
| 2 months ago | ||
| 2 years ago | ||
| 6 months ago | ||
| 2 months ago | ||
| 30 days ago | ||
| 2 months ago | ||
| 28 days ago | ||
| 1 month ago | ||
| 17 days ago | ||
| 17 days ago | ||
| 17 days ago |
高效的服务器意味着更低的基础设施成本、更强的负载响应能力,以及更满意的用户体验。如何在不牺牲安全验证和便捷开发的前提下,高效地管理服务器资源,以处理尽可能多的请求?
Fastify 应运而生。Fastify 是一个高度专注于提供最佳开发者体验、同时保持极低开销并具备强大插件架构的 Web 框架。它借鉴了 Hapi 和 Express 的设计理念,并且据我们所知,它是目前同类 Web 框架中速度最快的之一。
main 分支对应 Fastify v6 版本。若需 v5 版本,请查看 5.x 分支。
目录
快速开始
创建一个文件夹并将其设为当前工作目录:
mkdir my-app
cd my-app
使用 npm init 生成一个 Fastify 项目:
npm init fastify
安装依赖:
npm i
以开发模式启动应用:
npm run dev
生产模式下:
npm start
在底层,npm init 会下载并运行 Fastify Create,而后者又会使用 Fastify CLI 的生成功能。
安装
要在现有项目中将 Fastify 作为依赖项安装:
npm i fastify
示例
// Require the framework and instantiate it
// ESM
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJS
const fastify = require('fastify')({
logger: true
})
// Declare a route
fastify.get('/', (request, reply) => {
reply.send({ hello: 'world' })
})
// Run the server!
fastify.listen({ port: 3000 }, (err, address) => {
if (err) throw err
// Server is now listening on ${address}
})
使用 async-await:
// ESM
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})
// CommonJS
const fastify = require('fastify')({
logger: true
})
fastify.get('/', async (request, reply) => {
reply.type('application/json').code(200)
return { hello: 'world' }
})
fastify.listen({ port: 3000 }, (err, address) => {
if (err) throw err
// Server is now listening on ${address}
})
想了解更多信息吗?请前往 快速入门。如果您更喜欢通过阅读代码来学习,可以探索官方 示例。
注意
.listen默认绑定到本地主机localhost接口(127.0.0.1或::1,具体取决于操作系统配置)。如果您在容器(Docker、GCP 等)中运行 Fastify,可能需要绑定到0.0.0.0。监听所有接口时请务必谨慎,这会带来固有的 安全风险。更多信息请参见 文档。
核心特性
- 高性能:据我们所知,Fastify 是目前速度最快的 Web 框架之一,根据代码复杂度不同,每秒可处理超过 76,000 个请求。
- 可扩展:Fastify 可通过其钩子、插件和装饰器进行完全扩展。
- 基于 Schema:尽管并非强制要求,但我们建议使用 JSON Schema 来验证路由并序列化输出。Fastify 内部会将 Schema 编译为高性能函数。
- 日志:日志极为重要,但成本较高;我们选择了最佳的日志工具 Pino,以尽可能降低这一成本!
- 开发者友好:该框架设计得非常直观,能在不牺牲性能和安全性的前提下,助力开发者日常使用。
基准测试
机器配置:EX41S-SSD,Intel Core i7,4Ghz,64GB RAM,4 核/8 线程,SSD。
测试方法:执行 autocannon -c 100 -d 40 -p 10 localhost:3000 * 2 次,取第二次的平均值
| 框架 | 版本 | 是否包含路由 | 每秒请求数 |
|---|---|---|---|
| Express | 4.17.3 | ✓ | 14,200 |
| hapi | 20.2.1 | ✓ | 42,284 |
| Restify | 8.6.1 | ✓ | 50,363 |
| Koa | 2.13.0 | ✗ | 54,272 |
| Fastify | 4.0.0 | ✓ | 77,193 |
| - | |||
http.Server |
16.14.2 | ✗ | 74,513 |
这些基准测试使用 https://github.com/fastify/benchmarks 进行。这是一个综合性的“Hello World”基准测试,旨在评估框架的开销。每个框架对应用程序造成的开销取决于具体应用。如果性能对您很重要,请务必自行进行基准测试。
文档
快速开始指南服务器路由封装日志中间件钩子装饰器验证与序列化Fluent Schema生命周期响应请求错误内容类型解析器插件测试基准测试如何编写优秀的插件插件指南HTTP2长期支持TypeScript 和类型支持无服务器建议
生态系统
支持
请访问 Fastify 帮助 查看过往支持问题并提出新的支持问题。
Fastify 3 及更低版本已停止维护(EOL),将不再接收任何安全更新或错误修复。
Fastify 的合作伙伴 HeroDevs 在 https://herodevs.com/support/fastify-nes 为所有不受支持的版本提供商业安全修复。Fastify 支持的版本矩阵可在 长期支持 文档中找到。
贡献指南
无论是报告漏洞、探讨改进方向与新想法,还是参与代码编写,我们都欢迎来自各方人士的贡献。在提交拉取请求前,请务必阅读贡献指南。
团队成员
Fastify 的成功离不开强大社区的支持。以下团队成员按字母顺序排列。
首席维护者:
- Matteo Collina, https://x.com/matteocollina,https://www.npmjs.com/~matteo.collina
- Tomas Della Vedova, https://x.com/delvedor,https://www.npmjs.com/~delvedor
- KaKa Ng, https://www.npmjs.com/~climba03003
- Manuel Spigolon, https://x.com/manueomm,https://www.npmjs.com/~eomm
- James Sumners, https://x.com/jsumners79,https://www.npmjs.com/~jsumners
Fastify 核心团队
- Aras Abbasi, https://www.npmjs.com/~uzlopak
- Harry Brundage, https://x.com/harrybrundage,https://www.npmjs.com/~airhorns
- Matteo Collina, https://x.com/matteocollina,https://www.npmjs.com/~matteo.collina
- Gürgün Dayıoğlu, https://www.npmjs.com/~gurgunday
- Tomas Della Vedova, https://x.com/delvedor,https://www.npmjs.com/~delvedor
- Carlos Fuentes, https://x.com/metcoder95,https://www.npmjs.com/~metcoder95
- Vincent Le Goff
- Luciano Mammino, https://x.com/loige,https://www.npmjs.com/~lmammino
- Jean Michelet, https://www.npmjs.com/~jean-michelet
- KaKa Ng, https://www.npmjs.com/~climba03003
- Luis Orbaiceta, https://www.npmjs.com/~luisorbaiceta
- Maksim Sinik, https://x.com/maksimsinik,https://www.npmjs.com/~fox1t
- Manuel Spigolon, https://x.com/manueomm,https://www.npmjs.com/~eomm
- James Sumners, https://x.com/jsumners79,https://www.npmjs.com/~jsumners
Fastify 插件团队
- Harry Brundage, https://x.com/harrybrundage, https://www.npmjs.com/~airhorns
- Simone Busoli, https://x.com/simonebu, https://www.npmjs.com/~simoneb
- Dan Castillo, https://www.npmjs.com/~dancastillo
- Matteo Collina, https://x.com/matteocollina, https://www.npmjs.com/~matteo.collina
- Gürgün Dayıoğlu, https://www.npmjs.com/~gurgunday
- Tomas Della Vedova, https://x.com/delvedor, https://www.npmjs.com/~delvedor
- Carlos Fuentes, https://x.com/metcoder95, https://www.npmjs.com/~metcoder95
- Vincent Le Goff
- Jean Michelet, https://www.npmjs.com/~jean-michelet
- KaKa Ng, https://www.npmjs.com/~climba03003
- Maksim Sinik, https://x.com/maksimsinik, https://www.npmjs.com/~fox1t
- Frazer Smith, https://www.npmjs.com/~fdawgs
- Manuel Spigolon, https://x.com/manueomm, https://www.npmjs.com/~eomm
- Antonio Tripodi, https://www.npmjs.com/~tony133
荣誉贡献者
在 Fastify 生态系统特定领域有杰出贡献的贡献者,当他们决定从活跃贡献者团队中退任时,将由首席维护者邀请加入此团队。
- Tommaso Allevi, https://x.com/allevitommaso, https://www.npmjs.com/~allevo
- Ethan Arrowood, https://x.com/arrowoodtech, https://www.npmjs.com/~ethan_arrowood
- Çağatay Çalı, https://x.com/cagataycali, https://www.npmjs.com/~cagataycali
- David Mark Clements, https://x.com/davidmarkclem, https://www.npmjs.com/~davidmarkclements
- dalisoft, https://x.com/dalisoft, https://www.npmjs.com/~dalisoft
- Dustin Deus, https://x.com/dustindeus, https://www.npmjs.com/~starptech
- Denis Fäcke, https://x.com/serayaeryn, https://www.npmjs.com/~serayaeryn
- Rafael Gonzaga, https://x.com/_rafaelgss, https://www.npmjs.com/~rafaelgss
- Trivikram Kamat, https://x.com/trivikram, https://www.npmjs.com/~trivikr
- Ayoub El Khattabi, https://x.com/ayoubelkh, https://www.npmjs.com/~ayoubelk
- Cemre Mengu, https://x.com/cemremengu, https://www.npmjs.com/~cemremengu
- Salman Mitha, https://www.npmjs.com/~salmanm
- Nathan Woltman, https://x.com/NathanWoltman, https://www.npmjs.com/~nwoltman
主办方
我们是 OpenJS Foundation 中的一个 普通项目。
赞助商
通过成为 赞助商 来支持本项目! Fastify 设有 Open Collective 页面,我们在此接受并管理资金捐赠。
鸣谢
本项目承蒙以下机构慷慨赞助:
此列表包含所有支持一名或多名团队成员维护本项目的公司。
许可证
基于 MIT 许可证授权。
为方便起见,以下是我们所有生产依赖项的许可证列表:
- MIT
- ISC
- BSD-3-Clause
- BSD-2-Clause
Introduction
Fastify/fastify:Fastify 是一款高速、轻量级的 Node.js web 框架,致力于提升性能与降低资源消耗,同时兼具卓越的可扩展性。Fastify 支持 HTTP/2 和中间件插件机制,非常适合构建现代化的 Web 服务及 API。【此简介由AI生成】
Customize your domain