1 | <div align="center"> <a href="https://fastify.dev/">
|
2 | <img
|
3 | src="https://github.com/fastify/graphics/raw/HEAD/fastify-landscape-outlined.svg"
|
4 | width="650"
|
5 | height="auto"
|
6 | />
|
7 | </a>
|
8 | </div>
|
9 |
|
10 | <div align="center">
|
11 |
|
12 | [![CI](https://github.com/fastify/fastify/actions/workflows/ci.yml/badge.svg)](https://github.com/fastify/fastify/actions/workflows/ci.yml)
|
13 | [![Package Manager
|
14 | CI](https://github.com/fastify/fastify/workflows/package-manager-ci/badge.svg?branch=main)](https://github.com/fastify/fastify/actions/workflows/package-manager-ci.yml)
|
15 | [![Web
|
16 | SIte](https://github.com/fastify/fastify/workflows/website/badge.svg?branch=main)](https://github.com/fastify/fastify/actions/workflows/website.yml)
|
17 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
|
18 | [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/7585/badge)](https://bestpractices.coreinfrastructure.org/projects/7585)
|
19 |
|
20 | </div>
|
21 |
|
22 | <div align="center">
|
23 |
|
24 | [![NPM
|
25 | version](https://img.shields.io/npm/v/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
|
26 | [![NPM
|
27 | downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
|
28 | [![Security Responsible
|
29 | Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/fastify/fastify/blob/main/SECURITY.md)
|
30 | [![Discord](https://img.shields.io/discord/725613461949906985)](https://discord.gg/fastify)
|
31 | [![Contribute with Gitpod](https://img.shields.io/badge/Contribute%20with-Gitpod-908a85?logo=gitpod&color=blue)](https://gitpod.io/#https://github.com/fastify/fastify)
|
32 | ![Open Collective backers and sponsors](https://img.shields.io/opencollective/all/fastify)
|
33 |
|
34 | </div>
|
35 |
|
36 | <br />
|
37 |
|
38 | An efficient server implies a lower cost of the infrastructure, a better
|
39 | responsiveness under load and happy users. How can you efficiently handle the
|
40 | resources of your server, knowing that you are serving the highest number of
|
41 | requests as possible, without sacrificing security validations and handy
|
42 | development?
|
43 |
|
44 | Enter Fastify. Fastify is a web framework highly focused on providing the best
|
45 | developer experience with the least overhead and a powerful plugin architecture.
|
46 | It is inspired by Hapi and Express and as far as we know, it is one of the
|
47 | fastest web frameworks in town.
|
48 |
|
49 | The `main` branch refers to the Fastify `v4` release. Check out the
|
50 | [`v3.x` branch](https://github.com/fastify/fastify/tree/3.x) for `v3`.
|
51 |
|
52 |
|
53 |
|
54 | ### Table of Contents
|
55 |
|
56 | - [Quick start](#quick-start)
|
57 | - [Install](#install)
|
58 | - [Example](#example)
|
59 | - [Fastify v1.x and v2.x](#fastify-v1x-and-v2x)
|
60 | - [Core features](#core-features)
|
61 | - [Benchmarks](#benchmarks)
|
62 | - [Documentation](#documentation)
|
63 | - [Ecosystem](#ecosystem)
|
64 | - [Support](#support)
|
65 | - [Team](#team)
|
66 | - [Hosted by](#hosted-by)
|
67 | - [License](#license)
|
68 |
|
69 |
|
70 | ### Quick start
|
71 |
|
72 | Create a folder and make it your current working directory:
|
73 |
|
74 | ```sh
|
75 | mkdir my-app
|
76 | cd my-app
|
77 | ```
|
78 |
|
79 | Generate a fastify project with `npm init`:
|
80 |
|
81 | ```sh
|
82 | npm init fastify
|
83 | ```
|
84 |
|
85 | Install dependencies:
|
86 |
|
87 | ```sh
|
88 | npm i
|
89 | ```
|
90 |
|
91 | To start the app in dev mode:
|
92 |
|
93 | ```sh
|
94 | npm run dev
|
95 | ```
|
96 |
|
97 | For production mode:
|
98 |
|
99 | ```sh
|
100 | npm start
|
101 | ```
|
102 |
|
103 | Under the hood `npm init` downloads and runs [Fastify
|
104 | Create](https://github.com/fastify/create-fastify), which in turn uses the
|
105 | generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
|
106 |
|
107 |
|
108 | ### Install
|
109 |
|
110 | To install Fastify in an existing project as a dependency:
|
111 |
|
112 | Install with npm:
|
113 | ```sh
|
114 | npm i fastify
|
115 | ```
|
116 | Install with yarn:
|
117 | ```sh
|
118 | yarn add fastify
|
119 | ```
|
120 |
|
121 | ### Example
|
122 |
|
123 | ```js
|
124 | // Require the framework and instantiate it
|
125 |
|
126 | // ESM
|
127 | import Fastify from 'fastify'
|
128 |
|
129 | const fastify = Fastify({
|
130 | logger: true
|
131 | })
|
132 | // CommonJs
|
133 | const fastify = require('fastify')({
|
134 | logger: true
|
135 | })
|
136 |
|
137 | // Declare a route
|
138 | fastify.get('/', (request, reply) => {
|
139 | reply.send({ hello: 'world' })
|
140 | })
|
141 |
|
142 | // Run the server!
|
143 | fastify.listen({ port: 3000 }, (err, address) => {
|
144 | if (err) throw err
|
145 | // Server is now listening on ${address}
|
146 | })
|
147 | ```
|
148 |
|
149 | with async-await:
|
150 |
|
151 | ```js
|
152 | // ESM
|
153 | import Fastify from 'fastify'
|
154 |
|
155 | const fastify = Fastify({
|
156 | logger: true
|
157 | })
|
158 | // CommonJs
|
159 | const fastify = require('fastify')({
|
160 | logger: true
|
161 | })
|
162 |
|
163 | fastify.get('/', async (request, reply) => {
|
164 | reply.type('application/json').code(200)
|
165 | return { hello: 'world' }
|
166 | })
|
167 |
|
168 | fastify.listen({ port: 3000 }, (err, address) => {
|
169 | if (err) throw err
|
170 | // Server is now listening on ${address}
|
171 | })
|
172 | ```
|
173 |
|
174 | Do you want to know more? Head to the <a
|
175 | href="./docs/Guides/Getting-Started.md"><code><b>Getting Started</b></code></a>.
|
176 |
|
177 | > ## Note
|
178 | > `.listen` binds to the local host, `localhost`, interface by default
|
179 | > (`127.0.0.1` or `::1`, depending on the operating system configuration). If
|
180 | > you are running Fastify in a container (Docker,
|
181 | > [GCP](https://cloud.google.com/), etc.), you may need to bind to `0.0.0.0`. Be
|
182 | > careful when deciding to listen on all interfaces; it comes with inherent
|
183 | > [security
|
184 | > risks](https://web.archive.org/web/20170711105010/https://snyk.io/blog/mongodb-hack-and-secure-defaults/).
|
185 | > See [the documentation](./docs/Reference/Server.md#listen) for more
|
186 | > information.
|
187 |
|
188 | ### Core features
|
189 |
|
190 | - **Highly performant:** as far as we know, Fastify is one of the fastest web
|
191 | frameworks in town, depending on the code complexity we can serve up to 76+
|
192 | thousand requests per second.
|
193 | - **Extensible:** Fastify is fully extensible via its hooks, plugins and
|
194 | decorators.
|
195 | - **Schema based:** even if it is not mandatory we recommend to use [JSON
|
196 | Schema](https://json-schema.org/) to validate your routes and serialize your
|
197 | outputs, internally Fastify compiles the schema in a highly performant
|
198 | function.
|
199 | - **Logging:** logs are extremely important but are costly; we chose the best
|
200 | logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)!
|
201 | - **Developer friendly:** the framework is built to be very expressive and help
|
202 | the developer in their daily use, without sacrificing performance and
|
203 | security.
|
204 |
|
205 | ### Benchmarks
|
206 |
|
207 | __Machine:__ EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.
|
208 |
|
209 | __Method:__: `autocannon -c 100 -d 40 -p 10 localhost:3000` * 2, taking the
|
210 | second average
|
211 |
|
212 | | Framework | Version | Router? | Requests/sec |
|
213 | | :----------------- | :------------------------- | :----------: | ------------: |
|
214 | | Express | 4.17.3 | ✓ | 14,200 |
|
215 | | hapi | 20.2.1 | ✓ | 42,284 |
|
216 | | Restify | 8.6.1 | ✓ | 50,363 |
|
217 | | Koa | 2.13.0 | ✗ | 54,272 |
|
218 | | **Fastify** | **4.0.0** | **✓** | **77,193** |
|
219 | | - | | | |
|
220 | | `http.Server` | 16.14.2 | ✗ | 74,513 |
|
221 |
|
222 | Benchmarks taken using https://github.com/fastify/benchmarks. This is a
|
223 | synthetic, "hello world" benchmark that aims to evaluate the framework overhead.
|
224 | The overhead that each framework has on your application depends on your
|
225 | application, you should __always__ benchmark if performance matters to you.
|
226 |
|
227 | ## Documentation
|
228 | * [__`Getting Started`__](./docs/Guides/Getting-Started.md)
|
229 | * [__`Guides`__](./docs/Guides/Index.md)
|
230 | * [__`Server`__](./docs/Reference/Server.md)
|
231 | * [__`Routes`__](./docs/Reference/Routes.md)
|
232 | * [__`Encapsulation`__](./docs/Reference/Encapsulation.md)
|
233 | * [__`Logging`__](./docs/Reference/Logging.md)
|
234 | * [__`Middleware`__](./docs/Reference/Middleware.md)
|
235 | * [__`Hooks`__](./docs/Reference/Hooks.md)
|
236 | * [__`Decorators`__](./docs/Reference/Decorators.md)
|
237 | * [__`Validation and Serialization`__](./docs/Reference/Validation-and-Serialization.md)
|
238 | * [__`Fluent Schema`__](./docs/Guides/Fluent-Schema.md)
|
239 | * [__`Lifecycle`__](./docs/Reference/Lifecycle.md)
|
240 | * [__`Reply`__](./docs/Reference/Reply.md)
|
241 | * [__`Request`__](./docs/Reference/Request.md)
|
242 | * [__`Errors`__](./docs/Reference/Errors.md)
|
243 | * [__`Content Type Parser`__](./docs/Reference/ContentTypeParser.md)
|
244 | * [__`Plugins`__](./docs/Reference/Plugins.md)
|
245 | * [__`Testing`__](./docs/Guides/Testing.md)
|
246 | * [__`Benchmarking`__](./docs/Guides/Benchmarking.md)
|
247 | * [__`How to write a good plugin`__](./docs/Guides/Write-Plugin.md)
|
248 | * [__`Plugins Guide`__](./docs/Guides/Plugins-Guide.md)
|
249 | * [__`HTTP2`__](./docs/Reference/HTTP2.md)
|
250 | * [__`Long Term Support`__](./docs/Reference/LTS.md)
|
251 | * [__`TypeScript and types support`__](./docs/Reference/TypeScript.md)
|
252 | * [__`Serverless`__](./docs/Guides/Serverless.md)
|
253 | * [__`Recommendations`__](./docs/Guides/Recommendations.md)
|
254 |
|
255 | 中文文档[地址](https://github.com/fastify/docs-chinese/blob/HEAD/README.md)
|
256 |
|
257 | ## Ecosystem
|
258 |
|
259 | - [Core](./docs/Guides/Ecosystem.md#core) - Core plugins maintained by the
|
260 | _Fastify_ [team](#team).
|
261 | - [Community](./docs/Guides/Ecosystem.md#community) - Community supported
|
262 | plugins.
|
263 | - [Live Examples](https://github.com/fastify/example) - Multirepo with a broad
|
264 | set of real working examples.
|
265 | - [Discord](https://discord.gg/D3FZYPy) - Join our discord server and chat with
|
266 | the maintainers.
|
267 |
|
268 | ## Support
|
269 | Please visit [Fastify help](https://github.com/fastify/help) to view prior
|
270 | support issues and to ask new support questions.
|
271 |
|
272 | ## Contributing
|
273 |
|
274 | Whether reporting bugs, discussing improvements and new ideas or writing code,
|
275 | we welcome contributions from anyone and everyone. Please read the [CONTRIBUTING](./CONTRIBUTING.md)
|
276 | guidelines before submitting pull requests.
|
277 |
|
278 | ## Team
|
279 |
|
280 | _Fastify_ is the result of the work of a great community. Team members are
|
281 | listed in alphabetical order.
|
282 |
|
283 | **Lead Maintainers:**
|
284 | * [__Matteo Collina__](https://github.com/mcollina),
|
285 | <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
286 | * [__Tomas Della Vedova__](https://github.com/delvedor),
|
287 | <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
288 | * [__Manuel Spigolon__](https://github.com/eomm),
|
289 | <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
|
290 | * [__James Sumners__](https://github.com/jsumners),
|
291 | <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
|
292 |
|
293 | ### Fastify Core team
|
294 | * [__Tommaso Allevi__](https://github.com/allevo),
|
295 | <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
|
296 | * [__Harry Brundage__](https://github.com/airhorns/),
|
297 | <https://twitter.com/harrybrundage>, <https://www.npmjs.com/~airhorns>
|
298 | * [__David Mark Clements__](https://github.com/davidmarkclements),
|
299 | <https://twitter.com/davidmarkclem>,
|
300 | <https://www.npmjs.com/~davidmarkclements>
|
301 | * [__Matteo Collina__](https://github.com/mcollina),
|
302 | <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
303 | * [__Tomas Della Vedova__](https://github.com/delvedor),
|
304 | <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
305 | * [__Dustin Deus__](https://github.com/StarpTech),
|
306 | <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
|
307 | * [__Ayoub El Khattabi__](https://github.com/AyoubElk),
|
308 | <https://twitter.com/ayoubelkh>, <https://www.npmjs.com/~ayoubelk>
|
309 | * [__Denis Fäcke__](https://github.com/SerayaEryn),
|
310 | <https://twitter.com/serayaeryn>, <https://www.npmjs.com/~serayaeryn>
|
311 | * [__Carlos Fuentes__](https://github.com/metcoder95),
|
312 | <https://twitter.com/metcoder95>, <https://www.npmjs.com/~metcoder95>
|
313 | * [__Rafael Gonzaga__](https://github.com/rafaelgss),
|
314 | <https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
|
315 | * [__Vincent Le Goff__](https://github.com/zekth)
|
316 | * [__Luciano Mammino__](https://github.com/lmammino),
|
317 | <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
|
318 | * [__Luis Orbaiceta__](https://github.com/luisorbaiceta),
|
319 | <https://twitter.com/luisorbai>, <https://www.npmjs.com/~luisorbaiceta>
|
320 | * [__Maksim Sinik__](https://github.com/fox1t),
|
321 | <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
|
322 | * [__Manuel Spigolon__](https://github.com/eomm),
|
323 | <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
|
324 | * [__James Sumners__](https://github.com/jsumners),
|
325 | <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
|
326 | * [__Aras Abbasi__](https://github.com/uzlopak),
|
327 | <https://www.npmjs.com/~uzlopak>
|
328 | * [__Gürgün Dayıoğlu__](https://github.com/gurgunday),
|
329 | <https://www.npmjs.com/~gurgunday>
|
330 |
|
331 | ### Fastify Plugins team
|
332 | * [__Matteo Collina__](https://github.com/mcollina),
|
333 | <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
334 | * [__Harry Brundage__](https://github.com/airhorns/),
|
335 | <https://twitter.com/harrybrundage>, <https://www.npmjs.com/~airhorns>
|
336 | * [__Tomas Della Vedova__](https://github.com/delvedor),
|
337 | <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
338 | * [__Ayoub El Khattabi__](https://github.com/AyoubElk),
|
339 | <https://twitter.com/ayoubelkh>, <https://www.npmjs.com/~ayoubelk>
|
340 | * [__Carlos Fuentes__](https://github.com/metcoder95),
|
341 | <https://twitter.com/metcoder95>, <https://www.npmjs.com/~metcoder95>
|
342 | * [__Vincent Le Goff__](https://github.com/zekth)
|
343 | * [__Salman Mitha__](https://github.com/salmanm),
|
344 | <https://www.npmjs.com/~salmanm>
|
345 | * [__Maksim Sinik__](https://github.com/fox1t),
|
346 | <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
|
347 | * [__Frazer Smith__](https://github.com/Fdawgs), <https://www.npmjs.com/~fdawgs>
|
348 | * [__Manuel Spigolon__](https://github.com/eomm),
|
349 | <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
|
350 | * [__Simone Busoli__](https://github.com/simoneb),
|
351 | <https://twitter.com/simonebu>, <https://www.npmjs.com/~simoneb>
|
352 | * [__Gürgün Dayıoğlu__](https://github.com/gurgunday),
|
353 | <https://www.npmjs.com/~gurgunday>
|
354 |
|
355 | ### Great Contributors
|
356 | Great contributors on a specific area in the Fastify ecosystem will be invited
|
357 | to join this group by Lead Maintainers.
|
358 |
|
359 | * [__dalisoft__](https://github.com/dalisoft), <https://twitter.com/dalisoft>,
|
360 | <https://www.npmjs.com/~dalisoft>
|
361 | * [__Luciano Mammino__](https://github.com/lmammino),
|
362 | <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
|
363 | * [__Evan Shortiss__](https://github.com/evanshortiss),
|
364 | <https://twitter.com/evanshortiss>, <https://www.npmjs.com/~evanshortiss>
|
365 |
|
366 | **Past Collaborators**
|
367 | * [__Çağatay Çalı__](https://github.com/cagataycali),
|
368 | <https://twitter.com/cagataycali>, <https://www.npmjs.com/~cagataycali>
|
369 | * [__Trivikram Kamat__](https://github.com/trivikr),
|
370 | <https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
|
371 | * [__Cemre Mengu__](https://github.com/cemremengu),
|
372 | <https://twitter.com/cemremengu>, <https://www.npmjs.com/~cemremengu>
|
373 | * [__Nathan Woltman__](https://github.com/nwoltman),
|
374 | <https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
|
375 | * [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/),
|
376 | <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
|
377 |
|
378 | ## Hosted by
|
379 |
|
380 | [<img
|
381 | src="https://github.com/openjs-foundation/artwork/blob/main/openjs_foundation/openjs_foundation-logo-horizontal-color.png?raw=true"
|
382 | width="250px;"/>](https://openjsf.org/projects)
|
383 |
|
384 | We are a [At-Large
|
385 | Project](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/PROJECT_PROGRESSION.md#at-large-projects)
|
386 | in the [OpenJS Foundation](https://openjsf.org/).
|
387 |
|
388 | ## Sponsors
|
389 |
|
390 | Support this project by becoming a [SPONSOR](./SPONSORS.md)!
|
391 | Fastify has an [Open Collective](https://opencollective.com/fastify)
|
392 | page where we accept and manage financial contributions.
|
393 |
|
394 | ## Acknowledgements
|
395 |
|
396 | This project is kindly sponsored by:
|
397 | - [NearForm](https://nearform.com)
|
398 | - [Platformatic](https://platformatic.dev)
|
399 |
|
400 | Past Sponsors:
|
401 | - [LetzDoIt](https://www.letzdoitapp.com/)
|
402 |
|
403 | This list includes all companies that support one or more of the team members
|
404 | in the maintenance of this project.
|
405 |
|
406 | ## License
|
407 |
|
408 | Licensed under [MIT](./LICENSE).
|
409 |
|
410 | For your convenience, here is a list of all the licenses of our production
|
411 | dependencies:
|
412 | - MIT
|
413 | - ISC
|
414 | - BSD-3-Clause
|
415 | - BSD-2-Clause
|