UNPKG

13 kBMarkdownView Raw
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)](http://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
21Disclosure](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
28An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users.
29How 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
31Enter 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
35Node.js v10 LTS (10.16.0) or later.
36
37### Quick start
38
39Create a folder and make it your current working directory:
40
41```
42mkdir my-app
43cd my-app
44```
45
46Generate a fastify project with `npm init`:
47
48```sh
49npm init fastify
50```
51
52Install dependencies:
53
54```js
55npm install
56```
57
58To start the app in dev mode:
59
60```sh
61npm run dev
62```
63
64For production mode:
65
66```sh
67npm start
68```
69
70Under the hood `npm init` downloads and runs [Fastify Create](https://github.com/fastify/create-fastify),
71which in turn uses the generate functionality of [Fastify CLI](https://github.com/fastify/fastify-cli).
72
73
74### Install
75
76If installing in an existing project, then Fastify can be installed into the project as a dependency:
77
78Install with npm:
79```
80npm i fastify --save
81```
82Install with yarn:
83```
84yarn add fastify
85```
86
87### Example
88
89```js
90// Require the framework and instantiate it
91const fastify = require('fastify')({
92 logger: true
93})
94
95// Declare a route
96fastify.get('/', (request, reply) => {
97 reply.send({ hello: 'world' })
98})
99
100// Run the server!
101fastify.listen(3000, (err, address) => {
102 if (err) throw err
103 fastify.log.info(`server listening on ${address}`)
104})
105```
106
107with async-await:
108
109```js
110const fastify = require('fastify')({
111 logger: true
112})
113
114fastify.get('/', async (request, reply) => {
115 reply.type('application/json').code(200)
116 return { hello: 'world' }
117})
118
119fastify.listen(3000, (err, address) => {
120 if (err) throw err
121 fastify.log.info(`server listening on ${address}`)
122})
123```
124
125Do 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
130Code 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`**.
131In 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](http://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 | &#10003; | 15,978 |
154| hapi | 19.1.0 | &#10003; | 45,815 |
155| Restify | 8.5.1 | &#10003; | 49,279 |
156| Koa | 2.13.0 | &#10007; | 54,848 |
157| **Fastify** | **3.0.0** | **&#10003;** | **78,956** |
158| - | | | |
159| `http.Server` | 12.18.2 | &#10007; | 70,380 |
160
161Benchmarks taken using https://github.com/fastify/benchmarks. This is a
162synthetic, "hello world" benchmark that aims to evaluate the framework
163overhead. The overhead that each framework has on your application
164depends on your application, you should __always__ benchmark if performance
165matters 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/Logging.md"><code><b>Logging</b></code></a>
172* <a href="./docs/Middleware.md"><code><b>Middleware</b></code></a>
173* <a href="./docs/Hooks.md"><code><b>Hooks</b></code></a>
174* <a href="./docs/Decorators.md"><code><b>Decorators</b></code></a>
175* <a href="./docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
176* <a href="./docs/Fluent-Schema.md"><code><b>Fluent Schema</b></code></a>
177* <a href="./docs/Lifecycle.md"><code><b>Lifecycle</b></code></a>
178* <a href="./docs/Reply.md"><code><b>Reply</b></code></a>
179* <a href="./docs/Request.md"><code><b>Request</b></code></a>
180* <a href="./docs/Errors.md"><code><b>Errors</b></code></a>
181* <a href="./docs/ContentTypeParser.md"><code><b>Content Type Parser</b></code></a>
182* <a href="./docs/Plugins.md"><code><b>Plugins</b></code></a>
183* <a href="./docs/Testing.md"><code><b>Testing</b></code></a>
184* <a href="./docs/Benchmarking.md"><code><b>Benchmarking</b></code></a>
185* <a href="./docs/Write-Plugin.md"><code><b>How to write a good plugin</b></code></a>
186* <a href="./docs/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
187* <a href="./docs/HTTP2.md"><code><b>HTTP2</b></code></a>
188* <a href="./docs/LTS.md"><code><b>Long Term Support</b></code></a>
189* <a href="./docs/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
190* <a href="./docs/Serverless.md"><code><b>Serverless</b></code></a>
191* <a href="./docs/Recommendations.md"><code><b>Recommendations</b></code></a>
192
193中文文档[地址](https://github.com/fastify/docs-chinese/blob/master/README.md)
194
195## Ecosystem
196
197- [Core](./docs/Ecosystem.md#core) - Core plugins maintained by the _Fastify_ [team](#team).
198- [Community](./docs/Ecosystem.md#community) - Community supported plugins.
199- [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
200- [Discord](https://discord.gg/D3FZYPy) - Join our discord server and chat with the maintainers.
201
202## Support
203Please visit [Fastify help](https://github.com/fastify/help) to view prior
204support issues and to ask new support questions.
205
206## Team
207
208_Fastify_ is the result of the work of a great community.
209Team members are listed in alphabetical order.
210
211**Lead Maintainers:**
212* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
213* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
214
215### Fastify Core team
216* [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
217* [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/), <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
218* [__David Mark Clements__](https://github.com/davidmarkclements), <https://twitter.com/davidmarkclem>, <https://www.npmjs.com/~davidmarkclements>
219* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
220* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
221* [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
222* [__Denis Fäcke__](https://github.com/SerayaEryn), <https://twitter.com/serayaeryn>, <https://www.npmjs.com/~serayaeryn>
223* [__Rafael Gonzaga__](https://github.com/rafaelgss), <https://twitter.com/_rafaelgss>, <https://www.npmjs.com/~rafaelgss>
224* [__Vincent Le Goff__](https://github.com/zekth)
225* [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
226* [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
227* [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
228* [__James Sumners__](https://github.com/jsumners), <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
229
230### Fastify Plugins team
231* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
232* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
233* [__Vincent Le Goff__](https://github.com/zekth)
234* [__Maksim Sinik__](https://github.com/fox1t), <https://twitter.com/maksimsinik>, <https://www.npmjs.com/~fox1t>
235* [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
236
237### Great Contributors
238Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.
239
240* [__dalisoft__](https://github.com/dalisoft), <https://twitter.com/dalisoft>, <https://www.npmjs.com/~dalisoft>
241* [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
242* [__Evan Shortiss__](https://github.com/evanshortiss), <https://twitter.com/evanshortiss>, <https://www.npmjs.com/~evanshortiss>
243
244**Past Collaborators**
245* [__Çağatay Çalı__](https://github.com/cagataycali), <https://twitter.com/cagataycali>, <https://www.npmjs.com/~cagataycali>
246* [__Trivikram Kamat__](https://github.com/trivikr), <https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
247* [__Cemre Mengu__](https://github.com/cemremengu), <https://twitter.com/cemremengu>, <https://www.npmjs.com/~cemremengu>
248* [__Nathan Woltman__](https://github.com/nwoltman), <https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
249
250## Hosted by
251
252[<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)
253
254We 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/).
255
256## Acknowledgements
257
258This project is kindly sponsored by:
259- [nearForm](http://nearform.com)
260
261Past Sponsors:
262- [LetzDoIt](http://www.letzdoitapp.com/)
263
264## License
265
266Licensed under [MIT](./LICENSE).
267
268For your convenience, here is a list of all the licenses of our production dependencies:
269- MIT
270- ISC
271- BSD-3-Clause
272- BSD-2-Clause