3 category: JavaScript libraries
7 [Fastify](https://github.com/fastify/fastify) lets you create HTTP servers in Node.js with good performance. This guide targets fastify v0.28.x.
16 const fastify = require('fastify')()
18 fastify.get('/', (req, reply) => {
19 reply.send({ hello: 'world' })
22 fastify.listen(3000, err => {
24 const port = fastify.server.address().port
25 console.log(`server listening on ${port}`)
34 fastify.register(require('./route'))
40 function (fastify, opts, next) {
41 fastify.get('/', (req, reply) => {
42 reply.send({ hello: 'world' })
49 Compose your app functionality into plugins. Plugins are simply functions.
51 See: [Plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins.md)
62 handler: (req, reply) => { ··· }
63 beforeHandler: (req, reply, done) => { ··· }
67 ### Shorthand declarations
70 fastify.get(path, [options], handler)
82 fastify.get('/', options, async (req, reply) => {
89 When using async functions, you can either `return` data or use `reply.send`.
101 request.req // Node.js core
102 request.log.info('hello')
105 See: [Request](https://github.com/fastify/fastify/blob/master/docs/Request.md)
109 #### Response headers
113 reply.header('Content-Type', 'text/html')
114 reply.type('text/html')
120 reply.redirect('/foo')
121 reply.redirect(302, '/foo')
128 reply.sent // → true|false
131 See: [Reply](https://github.com/fastify/fastify/blob/master/docs/Reply.md)
135 #### Define a JSON schema
140 name: { type: 'string' },
141 excitement: { type: 'integer' }
147 hello: { type: 'string' }
154 #### Pass it to the route
157 fastify.get('/', { schema }, (req, reply) => {
163 #### or (same as above)
170 handler: (req, reply) => { ··· }
175 By defining a JSON schema, you get validation and improved performance.
177 See: [Validation and serialize](https://github.com/fastify/fastify/blob/master/docs/Validation-And-Serialize.md)
187 err => { if (err) throw err }
195 module.exports = (fastify, options, next) => {
196 fastify.get('/', ···)
202 See: [Register](https://github.com/fastify/fastify/blob/master/docs/Getting-Started.md#register)
208 require('./another-route'),
209 require('./yet-another-route')
215 You can pass arrays to `register()`.
217 ### Register with prefix
226 This prefixes all routes in that module.
231 const helmet = require('fastify-helmet')
233 fastify.register(helmet)
236 See: [fastify-helmet](https://github.com/fastify/fastify-helmet)
241 const fp = require('fastify-plugin')
243 module.exports = fp((fastify, opts, next) => {
245 fastify.decorate('utility', () => {})
251 Allows you to limit Fastify versions via semver, and allows you not make a new Fastify scope.
253 See: [fastify-plugin](https://github.com/fastify/fastify-plugin)
263 fastify.use(require('cors')())
264 fastify.use(require('dns-prefetch-control')())
265 fastify.use(require('frameguard')())
266 fastify.use(require('hide-powered-by')())
267 fastify.use(require('hsts')())
268 fastify.use(require('ienoopen')())
269 fastify.use(require('x-xss-protection')())
272 Compatible with Express and Restify middlewares. (Don't use these middleware, these are covered by [fastify-helmet](https://github.com/fastify/fastify-helmet).)
274 See: [Middlewares](https://github.com/fastify/fastify/blob/master/docs/Middlewares.md)
282 const fastify = require('fastify')()
284 fastify.register(require('point-of-view'), {
293 fastify.get('/', (req, reply) => {
294 reply.view('/templates/index.ejs', { text: 'text' })
299 Support `ejs`, `pug`, `handlebars` and `marko`.
301 See: [point-of-view](https://github.com/fastify/point-of-view)
306 fastify.register(require('point-of-view'), {
310 templates: '/templates',
315 `templates` lets you update the templates folder. `options` are options passed onto the template engines.