3 category: JavaScript libraries
13 [Fastify](https://github.com/fastify/fastify) lets you create HTTP servers in Node.js with good performance. This guide targets fastify v0.28.x.
15 - [Fastify source code](https://github.com/fastify/fastify) _(github.com)_
16 - [Documentation](https://github.com/fastify/fastify#documentation) _(github.com)_
22 const fastify = require('fastify')()
24 fastify.get('/', (req, reply) => {
25 reply.send({ hello: 'world' })
28 fastify.listen(3000, err => {
30 const port = fastify.server.address().port
31 console.log(`server listening on ${port}`)
40 fastify.register(require('./route'))
46 function (fastify, opts, next) {
47 fastify.get('/', (req, reply) => {
48 reply.send({ hello: 'world' })
55 Compose your app functionality into plugins. Plugins are simply functions.
57 See: [Plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins.md)
68 handler: (req, reply) => { ··· }
69 beforeHandler: (req, reply, done) => { ··· }
73 ### Shorthand declarations
76 fastify.get(path, [options], handler)
88 fastify.get('/', options, async (req, reply) => {
95 When using async functions, you can either `return` data or use `reply.send`.
107 request.req // Node.js core
108 request.log.info('hello')
111 See: [Request](https://github.com/fastify/fastify/blob/master/docs/Request.md)
115 #### Response headers
119 reply.header('Content-Type', 'text/html')
120 reply.type('text/html')
126 reply.redirect('/foo')
127 reply.redirect(302, '/foo')
134 reply.sent // → true|false
137 See: [Reply](https://github.com/fastify/fastify/blob/master/docs/Reply.md)
141 #### Define a JSON schema
146 name: { type: 'string' },
147 excitement: { type: 'integer' }
153 hello: { type: 'string' }
160 #### Pass it to the route
163 fastify.get('/', { schema }, (req, reply) => {
169 #### or (same as above)
176 handler: (req, reply) => { ··· }
181 By defining a JSON schema, you get validation and improved performance.
183 See: [Validation and serialization](https://github.com/fastify/fastify/blob/master/docs/Validation-and-Serialization.md)
193 err => { if (err) throw err }
201 module.exports = (fastify, options, next) => {
202 fastify.get('/', ···)
208 See: [Register](https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md#register)
214 require('./another-route'),
215 require('./yet-another-route')
221 You can pass arrays to `register()`.
223 ### Register with prefix
232 This prefixes all routes in that module.
237 const helmet = require('fastify-helmet')
239 fastify.register(helmet)
242 See: [fastify-helmet](https://github.com/fastify/fastify-helmet)
247 const fp = require('fastify-plugin')
249 module.exports = fp((fastify, opts, next) => {
251 fastify.decorate('utility', () => {})
257 Allows you to limit Fastify versions via semver, and allows you not make a new Fastify scope.
259 See: [fastify-plugin](https://github.com/fastify/fastify-plugin)
269 fastify.use(require('cors')())
270 fastify.use(require('dns-prefetch-control')())
271 fastify.use(require('frameguard')())
272 fastify.use(require('hide-powered-by')())
273 fastify.use(require('hsts')())
274 fastify.use(require('ienoopen')())
275 fastify.use(require('x-xss-protection')())
278 Compatible with Express and Restify middlewares. (Don't use these middleware, these are covered by [fastify-helmet](https://github.com/fastify/fastify-helmet).)
280 See: [Middlewares](https://github.com/fastify/fastify/blob/master/docs/Middleware.md)
288 const fastify = require('fastify')()
290 fastify.register(require('point-of-view'), {
299 fastify.get('/', (req, reply) => {
300 reply.view('/templates/index.ejs', { text: 'text' })
305 Support `ejs`, `pug`, `handlebars` and `marko`.
307 See: [point-of-view](https://github.com/fastify/point-of-view)
312 fastify.register(require('point-of-view'), {
316 templates: '/templates',
321 `templates` lets you update the templates folder. `options` are options passed onto the template engines.