1 | <div align="center">
|
2 | <img src="https://github.com/fastify/graphics/raw/master/full-logo.png" width="650" height="auto"/>
|
3 | </div>
|
4 |
|
5 | <div align="center">
|
6 |
|
7 | ![](https://github.com/fastify/fastify/workflows/ci/badge.svg)
|
8 | ![](https://github.com/fastify/fastify/workflows/package-manager-ci/badge.svg)
|
9 | ![](https://github.com/fastify/fastify/workflows/website/badge.svg)
|
10 | [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify/badge.svg)](https://snyk.io/test/github/fastify/fastify)
|
11 | [![Coverage Status](https://coveralls.io/repos/github/fastify/fastify/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify?branch=master)
|
12 | [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
|
13 |
|
14 | </div>
|
15 |
|
16 | <div align="center">
|
17 |
|
18 | [![NPM version](https://img.shields.io/npm/v/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
|
19 | [![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
|
20 | [![Security Responsible
|
21 | Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md)
|
22 | [![Discord](https://img.shields.io/discord/725613461949906985)](https://discord.gg/D3FZYPy)
|
23 |
|
24 | </div>
|
25 |
|
26 | <br />
|
27 |
|
28 | An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users.
|
29 | How can you efficiently handle the resources of your server, knowing that you are serving the highest number of requests as possible, without sacrificing security validations and handy development?
|
30 |
|
31 | Enter Fastify. Fastify is a web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. It is inspired by Hapi and Express and as far as we know, it is one of the fastest web frameworks in town.
|
32 |
|
33 | ### Requirements
|
34 |
|
35 | Node.js v10 LTS (10.16.0) or later.
|
36 |
|
37 | ### Quick start
|
38 |
|
39 | Create a folder and make it your current working directory:
|
40 |
|
41 | ```sh
|
42 | mkdir my-app
|
43 | cd my-app
|
44 | ```
|
45 |
|
46 | Generate a fastify project with `npm init`:
|
47 |
|
48 | ```sh
|
49 | npm init fastify
|
50 | ```
|
51 |
|
52 | Install dependencies:
|
53 |
|
54 | ```sh
|
55 | npm install
|
56 | ```
|
57 |
|
58 | To start the app in dev mode:
|
59 |
|
60 | ```sh
|
61 | npm run dev
|
62 | ```
|
63 |
|
64 | For production mode:
|
65 |
|
66 | ```sh
|
67 | npm start
|
68 | ```
|
69 |
|
70 | Under the hood `npm init` downloads and runs [Fastify Create](https://github.com/fastify/create-fastify),
|
71 | which in turn uses the generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
|
72 |
|
73 |
|
74 | ### Install
|
75 |
|
76 | If installing in an existing project, then Fastify can be installed into the project as a dependency:
|
77 |
|
78 | Install with npm:
|
79 | ```sh
|
80 | npm i fastify --save
|
81 | ```
|
82 | Install with yarn:
|
83 | ```sh
|
84 | yarn add fastify
|
85 | ```
|
86 |
|
87 | ### Example
|
88 |
|
89 | ```js
|
90 | // Require the framework and instantiate it
|
91 | const fastify = require('fastify')({
|
92 | logger: true
|
93 | })
|
94 |
|
95 | // Declare a route
|
96 | fastify.get('/', (request, reply) => {
|
97 | reply.send({ hello: 'world' })
|
98 | })
|
99 |
|
100 | // Run the server!
|
101 | fastify.listen(3000, (err, address) => {
|
102 | if (err) throw err
|
103 | fastify.log.info(`server listening on ${address}`)
|
104 | })
|
105 | ```
|
106 |
|
107 | with async-await:
|
108 |
|
109 | ```js
|
110 | const fastify = require('fastify')({
|
111 | logger: true
|
112 | })
|
113 |
|
114 | fastify.get('/', async (request, reply) => {
|
115 | reply.type('application/json').code(200)
|
116 | return { hello: 'world' }
|
117 | })
|
118 |
|
119 | fastify.listen(3000, (err, address) => {
|
120 | if (err) throw err
|
121 | fastify.log.info(`server listening on ${address}`)
|
122 | })
|
123 | ```
|
124 |
|
125 | Do you want to know more? Head to the <a href="./docs/Getting-Started.md"><code><b>Getting Started</b></code></a>.
|
126 |
|
127 |
|
128 | ### Fastify v1.x and v2.x
|
129 |
|
130 | Code for Fastify's **v1.x** is in [**`branch 1.x`**](https://github.com/fastify/fastify/tree/1.x), so all Fastify 1.x related changes should be based on **`branch 1.x`**.
|
131 | In a similar way, all Fastify **v2.x** related changes should be based on [**`branch 2.x`**](https://github.com/fastify/fastify/tree/2.x).
|
132 |
|
133 | > ## Note
|
134 | > `.listen` binds to the local host, `localhost`, interface by default (`127.0.0.1` or `::1`, depending on the operating system configuration). If you are running Fastify in a container (Docker, [GCP](https://cloud.google.com/), etc.), you may need to bind to `0.0.0.0`. Be careful when deciding to listen on all interfaces; it comes with inherent [security risks](https://web.archive.org/web/20170711105010/https://snyk.io/blog/mongodb-hack-and-secure-defaults/).
|
135 | > See [the documentation](./docs/Server.md#listen) for more information.
|
136 |
|
137 | ### Core features
|
138 |
|
139 | - **Highly performant:** as far as we know, Fastify is one of the fastest web frameworks in town, depending on the code complexity we can serve up to 76+ thousand requests per second.
|
140 | - **Extendible:** Fastify is fully extensible via its hooks, plugins and decorators.
|
141 | - **Schema based:** even if it is not mandatory we recommend to use [JSON Schema](https://json-schema.org/) to validate your routes and serialize your outputs, internally Fastify compiles the schema in a highly performant function.
|
142 | - **Logging:** logs are extremely important but are costly; we chose the best logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)!
|
143 | - **Developer friendly:** the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.
|
144 |
|
145 | ### Benchmarks
|
146 |
|
147 | __Machine:__ EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.
|
148 |
|
149 | __Method:__: `autocannon -c 100 -d 40 -p 10 localhost:3000` * 2, taking the second average
|
150 |
|
151 | | Framework | Version | Router? | Requests/sec |
|
152 | | :----------------- | :------------------------- | :----------: | ------------: |
|
153 | | Express | 4.17.1 | ✓ | 15,978 |
|
154 | | hapi | 19.1.0 | ✓ | 45,815 |
|
155 | | Restify | 8.5.1 | ✓ | 49,279 |
|
156 | | Koa | 2.13.0 | ✗ | 54,848 |
|
157 | | **Fastify** | **3.0.0** | **✓** | **78,956** |
|
158 | | - | | | |
|
159 | | `http.Server` | 12.18.2 | ✗ | 70,380 |
|
160 |
|
161 | Benchmarks taken using https://github.com/fastify/benchmarks. This is a
|
162 | synthetic, "hello world" benchmark that aims to evaluate the framework
|
163 | overhead. The overhead that each framework has on your application
|
164 | depends on your application, you should __always__ benchmark if performance
|
165 | matters to you.
|
166 |
|
167 | ## Documentation
|
168 | * <a href="./docs/Getting-Started.md"><code><b>Getting Started</b></code></a>
|
169 | * <a href="./docs/Server.md"><code><b>Server</b></code></a>
|
170 | * <a href="./docs/Routes.md"><code><b>Routes</b></code></a>
|
171 | * <a href="./docs/Encapsulation.md"><code><b>Encapsulation</b></code></a>
|
172 | * <a href="./docs/Logging.md"><code><b>Logging</b></code></a>
|
173 | * <a href="./docs/Middleware.md"><code><b>Middleware</b></code></a>
|
174 | * <a href="./docs/Hooks.md"><code><b>Hooks</b></code></a>
|
175 | * <a href="./docs/Decorators.md"><code><b>Decorators</b></code></a>
|
176 | * <a href="./docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
|
177 | * <a href="./docs/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
|
178 | * <a href="./docs/Lifecycle.md"><code><b>Lifecycle</b></code></a>
|
179 | * <a href="./docs/Reply.md"><code><b>Reply</b></code></a>
|
180 | * <a href="./docs/Request.md"><code><b>Request</b></code></a>
|
181 | * <a href="./docs/Errors.md"><code><b>Errors</b></code></a>
|
182 | * <a href="./docs/ContentTypeParser.md"><code><b>Content Type Parser</b></code></a>
|
183 | * <a href="./docs/Plugins.md"><code><b>Plugins</b></code></a>
|
184 | * <a href="./docs/Testing.md"><code><b>Testing</b></code></a>
|
185 | * <a href="./docs/Benchmarking.md"><code><b>Benchmarking</b></code></a>
|
186 | * <a href="./docs/Write-Plugin.md"><code><b>How to write a good plugin</b></code></a>
|
187 | * <a href="./docs/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
|
188 | * <a href="./docs/HTTP2.md"><code><b>HTTP2</b></code></a>
|
189 | * <a href="./docs/LTS.md"><code><b>Long Term Support</b></code></a>
|
190 | * <a href="./docs/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
|
191 | * <a href="./docs/Serverless.md"><code><b>Serverless</b></code></a>
|
192 | * <a href="./docs/Recommendations.md"><code><b>Recommendations</b></code></a>
|
193 |
|
194 | 中文文档[地址](https://github.com/fastify/docs-chinese/blob/master/README.md)
|
195 |
|
196 | ## Ecosystem
|
197 |
|
198 | - [Core](./docs/Ecosystem.md#core) - Core plugins maintained by the _Fastify_ [team](#team).
|
199 | - [Community](./docs/Ecosystem.md#community) - Community supported plugins.
|
200 | - [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
|
201 | - [Discord](https://discord.gg/D3FZYPy) - Join our discord server and chat with the maintainers.
|
202 |
|
203 | ## Support
|
204 | Please visit [Fastify help](https://github.com/fastify/help) to view prior
|
205 | support issues and to ask new support questions.
|
206 |
|
207 | ## Team
|
208 |
|
209 | _Fastify_ is the result of the work of a great community.
|
210 | Team members are listed in alphabetical order.
|
211 |
|
212 | **Lead Maintainers:**
|
213 | * [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
214 | * [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
215 |
|
216 | ### Fastify Core team
|
217 | * [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
|
218 | * [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/), <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
|
219 | * [__David Mark Clements__](https://github.com/davidmarkclements), <https://twitter.com/davidmarkclem>, <https://www.npmjs.com/~davidmarkclements>
|
220 | * [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
221 | * [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
222 | * [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
|
223 | * [__Denis Fäcke__](https://github.com/SerayaEryn), <https://twitter.com/serayaeryn>, <https://www.npmjs.com/~serayaeryn>
|
224 | * [__Rafael Gonzaga__](https://github.com/rafaelgss), <https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
|
225 | * [__Vincent Le Goff__](https://github.com/zekth)
|
226 | * [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
|
227 | * [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
|
228 | * [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
|
229 | * [__James Sumners__](https://github.com/jsumners), <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
|
230 |
|
231 | ### Fastify Plugins team
|
232 | * [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
|
233 | * [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
|
234 | * [__Vincent Le Goff__](https://github.com/zekth)
|
235 | * [__Salman Mitha__](https://github.com/salmanm), <https://www.npmjs.com/~salmanm>
|
236 | * [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
|
237 | * [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
|
238 |
|
239 | ### Great Contributors
|
240 | Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.
|
241 |
|
242 | * [__dalisoft__](https://github.com/dalisoft), <https://twitter.com/dalisoft>, <https://www.npmjs.com/~dalisoft>
|
243 | * [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
|
244 | * [__Evan Shortiss__](https://github.com/evanshortiss), <https://twitter.com/evanshortiss>, <https://www.npmjs.com/~evanshortiss>
|
245 |
|
246 | **Past Collaborators**
|
247 | * [__Çağatay Çalı__](https://github.com/cagataycali), <https://twitter.com/cagataycali>, <https://www.npmjs.com/~cagataycali>
|
248 | * [__Trivikram Kamat__](https://github.com/trivikr), <https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
|
249 | * [__Cemre Mengu__](https://github.com/cemremengu), <https://twitter.com/cemremengu>, <https://www.npmjs.com/~cemremengu>
|
250 | * [__Nathan Woltman__](https://github.com/nwoltman), <https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
|
251 |
|
252 | ## Hosted by
|
253 |
|
254 | [<img src="https://github.com/openjs-foundation/cross-project-council/blob/master/logos/openjsf-color.png?raw=true" width="250px;"/>](https://openjsf.org/projects/#growth)
|
255 |
|
256 | We are a [Growth Project](https://github.com/openjs-foundation/cross-project-council/blob/master/PROJECT_PROGRESSION.md#growth-stage) in the [OpenJS Foundation](https://openjsf.org/).
|
257 |
|
258 | ## Acknowledgements
|
259 |
|
260 | This project is kindly sponsored by:
|
261 | - [nearForm](https://nearform.com)
|
262 |
|
263 | Past Sponsors:
|
264 | - [LetzDoIt](http://www.letzdoitapp.com/)
|
265 |
|
266 | ## License
|
267 |
|
268 | Licensed under [MIT](./LICENSE).
|
269 |
|
270 | For your convenience, here is a list of all the licenses of our production dependencies:
|
271 | - MIT
|
272 | - ISC
|
273 | - BSD-3-Clause
|
274 | - BSD-2-Clause
|