UNPKG

12.7 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[![Build Status](https://travis-ci.org/fastify/fastify.svg?branch=master)](https://travis-ci.org/fastify/fastify)
8[![Build Status](https://dev.azure.com/fastify/fastify/_apis/build/status/fastify.fastify)](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=1) [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify/badge.svg)](https://snyk.io/test/github/fastify/fastify)
9[![Coverage Status](https://coveralls.io/repos/github/fastify/fastify/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify?branch=master)
10[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
11
12</div>
13
14<div align="center">
15
16[![NPM version](https://img.shields.io/npm/v/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
17[![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/fastify)
18[![Security Responsible
19Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/nodejs/security-wg/blob/master/processes/responsible_disclosure_template.md)
20
21</div>
22
23<br />
24
25An efficient server implies a lower cost of the infrastructure, a better responsiveness under load and happy users.
26How 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?
27
28Enter 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.
29
30### Install
31
32Install with npm:
33```
34npm i fastify --save
35```
36Install with yarn:
37```
38yarn add fastify
39```
40
41### Example
42
43```js
44// Require the framework and instantiate it
45const fastify = require('fastify')({
46 logger: true
47})
48
49// Declare a route
50fastify.get('/', (request, reply) => {
51 reply.send({ hello: 'world' })
52})
53
54// Run the server!
55fastify.listen(3000, (err, address) => {
56 if (err) throw err
57 fastify.log.info(`server listening on ${address}`)
58})
59```
60
61with async-await:
62
63```js
64const fastify = require('fastify')({
65 logger: true
66})
67
68fastify.get('/', async (request, reply) => {
69 reply.type('application/json').code(200)
70 return { hello: 'world' }
71})
72
73fastify.listen(3000, (err, address) => {
74 if (err) throw err
75 fastify.log.info(`server listening on ${address}`)
76})
77```
78
79Do you want to know more? Head to the <a href="https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md"><code><b>Getting Started</b></code></a>.
80
81### Quick start with Fastify CLI
82
83Good tools make API development quicker and easier to maintain than doing everything manually.
84
85The [Fastify CLI](https://github.com/fastify/fastify-cli) is a command line interface tool that can create new projects, manage plugins, and perform a variety of development tasks testing and running the application.
86
87The goal in this guide is to build and run a simple Fastify project, using the [Fastify CLI](https://github.com/fastify/fastify-cli), while adhering to the Style Guide recommendations that benefit every Fastify project.
88
89### Example
90
91Open a terminal window.
92
93```
94npm install fastify-cli --global
95```
96
97Generate a new project and default app by running the following command:
98
99```
100fastify generate
101```
102
103For more information, see the [Fastify CLI documentation](https://github.com/fastify/fastify-cli).
104
105### Fastify v1.x
106
107Code 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`**.
108
109> ## Note
110> `.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/).
111> See [the documentation](https://github.com/fastify/fastify/blob/master/docs/Server.md#listen) for more information.
112
113### Core features
114
115- **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 30 thousand requests per second.
116- **Extendible:** Fastify is fully extensible via its hooks, plugins and decorators.
117- **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.
118- **Logging:** logs are extremely important but are costly; we chose the best logger to almost remove this cost, [Pino](https://github.com/pinojs/pino)!
119- **Developer friendly:** the framework is built to be very expressive and help the developer in their daily use, without sacrificing performance and security.
120
121### Benchmarks
122
123__Machine:__ EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD.
124
125__Method:__: `autocannon -c 100 -d 40 -p 10 localhost:3000` * 2, taking the second average
126
127| Framework | Version | Router? | Requests/sec |
128| :----------------- | :------------------------- | :----------: | ------------: |
129| hapi | 18.1.0 | &#10003; | 29,998 |
130| Express | 4.16.4 | &#10003; | 38,510 |
131| Restify | 8.0.0 | &#10003; | 39,331 |
132| Koa | 2.7.0 | &#10007; | 50,933 |
133| **Fastify** | **2.0.0** | **&#10003;** | **76,835** |
134| - | | | |
135| `http.Server` | 10.15.2 | &#10007; | 71,768 |
136
137Benchmarks taken using https://github.com/fastify/benchmarks. This is a
138synthetic, "hello world" benchmark that aims to evaluate the framework
139overhead. The overhead that each framework has on your application
140depends on your application, you should __always__ benchmark if performance
141matters to you.
142
143## Documentation
144* <a href="https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md"><code><b>Getting Started</b></code></a>
145* <a href="https://github.com/fastify/fastify/blob/master/docs/Server.md"><code><b>Server</b></code></a>
146* <a href="https://github.com/fastify/fastify/blob/master/docs/Routes.md"><code><b>Routes</b></code></a>
147* <a href="https://github.com/fastify/fastify/blob/master/docs/Logging.md"><code><b>Logging</b></code></a>
148* <a href="https://github.com/fastify/fastify/blob/master/docs/Middlewares.md"><code><b>Middlewares</b></code></a>
149* <a href="https://github.com/fastify/fastify/blob/master/docs/Hooks.md"><code><b>Hooks</b></code></a>
150* <a href="https://github.com/fastify/fastify/blob/master/docs/Decorators.md"><code><b>Decorators</b></code></a>
151* <a href="https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md"><code><b>Validation and Serialization</b></code></a>
152* <a href="https://github.com/fastify/fastify/blob/master/docs/Lifecycle.md"><code><b>Lifecycle</b></code></a>
153* <a href="https://github.com/fastify/fastify/blob/master/docs/Reply.md"><code><b>Reply</b></code></a>
154* <a href="https://github.com/fastify/fastify/blob/master/docs/Request.md"><code><b>Request</b></code></a>
155* <a href="https://github.com/fastify/fastify/blob/master/docs/Errors.md"><code><b>Errors</b></code></a>
156* <a href="https://github.com/fastify/fastify/blob/master/docs/ContentTypeParser.md"><code><b>Content Type Parser</b></code></a>
157* <a href="https://github.com/fastify/fastify/blob/master/docs/Plugins.md"><code><b>Plugins</b></code></a>
158* <a href="https://github.com/fastify/fastify/blob/master/docs/Testing.md"><code><b>Testing</b></code></a>
159* <a href="https://github.com/fastify/fastify/blob/master/docs/Benchmarking.md"><code><b>Benchmarking</b></code></a>
160* <a href="https://github.com/fastify/fastify/blob/master/docs/Write-Plugin.md"><code><b>How to write a good plugin</b></code></a>
161* <a href="https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md"><code><b>Plugins Guide</b></code></a>
162* <a href="https://github.com/fastify/fastify/blob/master/docs/HTTP2.md"><code><b>HTTP2</b></code></a>
163* <a href="https://github.com/fastify/fastify/blob/master/docs/LTS.md"><code><b>Long Term Support</b></code></a>
164* <a href="https://github.com/fastify/fastify/blob/master/docs/TypeScript.md"><code><b>TypeScript and types support</b></code></a>
165* <a href="https://github.com/fastify/fastify/blob/master/docs/Serverless.md"><code><b>Serverless</b></code></a>
166
167中文文档[地址](https://github.com/fastify/docs-chinese/blob/master/README.md)
168
169## Ecosystem
170- [Core](https://github.com/fastify/fastify/blob/master/docs/Ecosystem.md#core) - Core plugins maintained by the _Fastify_ [team](#team).
171- [Community](https://github.com/fastify/fastify/blob/master/docs/Ecosystem.md#community) - Community supported plugins.
172- [Live Examples](https://github.com/fastify/example) - Multirepo with a broad set of real working examples.
173
174## Support
175- [Fastify help](https://github.com/fastify/help)
176- [Gitter Chat](https://gitter.im/fastify)
177
178## Team
179
180_Fastify_ is the result of the work of a great community.
181Team members are listed in alphabetical order.
182
183**Lead Maintainers:**
184* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
185* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
186
187### Fastify Core team
188* [__Tommaso Allevi__](https://github.com/allevo), <https://twitter.com/allevitommaso>, <https://www.npmjs.com/~allevo>
189* [__Ethan Arrowood__](https://github.com/Ethan-Arrowood/), <https://twitter.com/arrowoodtech>, <https://www.npmjs.com/~ethan_arrowood>
190* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
191* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
192* [__Dustin Deus__](https://github.com/StarpTech), <https://twitter.com/dustindeus>, <https://www.npmjs.com/~starptech>
193* [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
194* [__Cemre Mengu__](https://github.com/cemremengu), <https://twitter.com/cemremengu>, <https://www.npmjs.com/~cemremengu>
195* [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
196* [__James Sumners__](https://github.com/jsumners), <https://twitter.com/jsumners79>, <https://www.npmjs.com/~jsumners>
197* [__Nathan Woltman__](https://github.com/nwoltman), <https://twitter.com/NathanWoltman>, <https://www.npmjs.com/~nwoltman>
198
199### Fastify Plugins team
200* [__Matteo Collina__](https://github.com/mcollina), <https://twitter.com/matteocollina>, <https://www.npmjs.com/~matteo.collina>
201* [__Tomas Della Vedova__](https://github.com/delvedor), <https://twitter.com/delvedor>, <https://www.npmjs.com/~delvedor>
202* [__Manuel Spigolon__](https://github.com/eomm), <https://twitter.com/manueomm>, <https://www.npmjs.com/~eomm>
203
204### Collaborators
205Great contributors on a specific area in the Fastify ecosystem will be invited to join this group by Lead Maintainers.
206
207* [__Luciano Mammino__](https://github.com/lmammino), <https://twitter.com/loige>, <https://www.npmjs.com/~lmammino>
208* [__Evan Shortiss__](https://github.com/evanshortiss), <https://twitter.com/evanshortiss>, <https://www.npmjs.com/~evanshortiss>
209
210**Past Collaborators**
211* [__Çağatay Çalı__](https://github.com/cagataycali), <https://twitter.com/cagataycali>, <https://www.npmjs.com/~cagataycali>
212* [__Trivikram Kamat__](https://github.com/trivikr), <https://twitter.com/trivikram>, <https://www.npmjs.com/~trivikr>
213
214## Acknowledgements
215
216This project is kindly sponsored by:
217- [nearForm](http://nearform.com)
218
219Past Sponsors:
220- [LetzDoIt](http://www.letzdoitapp.com/)
221
222## License
223
224Licensed under [MIT](./LICENSE).
225
226For your convenience, here is a list of all the licenses of our production dependencies:
227- MIT
228- ISC
229- BSD-3-Clause
230- BSD-2-Clause