Fastify is een uiterst efficiënt webframework met minimale overhead, gericht op een geweldige ontwikkelaarservaring en maximale doorvoersnelheid.
Aan de slag
Introductie & Documentatie
Fastify biedt uitstekende prestaties via schemagebaseerde compilatie en een modulaire plugin-architectuur.
- Fastify Source Code (github.com)
- Fastify Documentation (fastify.dev)
Hello World Voorbeeld
const fastify = require('fastify')()
fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.listen(3000, err => {
if (err) throw err
const port = fastify.server.address().port
console.log(`server listening on ${port}`)
})
Routing
Routeverklaring
fastify.route({
method: 'GET',
url: '/',
schema: { ··· },
handler: (req, reply) => { ··· },
beforeHandler: (req, reply, done) => { ··· }
})
Korte methoden (Shorthand)
fastify.get(path, [options], handler)
fastify.head(path, [options], handler)
fastify.post(path, [options], handler)
fastify.put(path, [options], handler)
fastify.delete(path, [options], handler)
fastify.options(path, [options], handler)
fastify.patch(path, [options], handler)
Async / Await Handlers
fastify.get('/', options, async (req, reply) => {
return data
// or
reply.send(data)
})
Met asynchrone handlers kunt u eenvoudigweg gegevens retourneren of reply.send(data) aanroepen.
Request & Reply
Request-object
request.query
request.body
request.params
request.headers
request.req // Node.js core incoming request
request.log.info('hello')
See: Request.
Reply-object
Antwoordheaders
reply.code(404)
reply.header('Content-Type', 'text/html')
reply.type('text/html')
Omleidingen
reply.redirect('/foo')
reply.redirect(302, '/foo')
Gegevens verzenden
reply.send(payload)
reply.sent // → true|false
See: Reply.
JSON-schemavalidatie
Schema definiëren
const schema = {
querystring: {
name: { type: 'string' },
excitement: { type: 'integer' }
},
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
Schema doorgeven aan route
fastify.get('/', { schema }, (req, reply) => {
···
})
Route-optieobject
fastify.route({
method: 'GET',
url: '/',
schema,
handler: (req, reply) => { ··· }
})
Het definiëren van een JSON-schema zorgt voor razendsnelle validatie en geoptimaliseerde serialisatie.
See: Validation and Serialization.
Plugins
Functie als plugin registreren
fastify.register(
require('./route'),
err => { if (err) throw err }
)
route.js
module.exports = (fastify, options, next) => {
fastify.get('/', (req, reply) => { ··· })
next()
}
See: Plugins.
Meerdere plugins registreren
fastify.register([
require('./another-route'),
require('./yet-another-route')
], opts, (err) => {
if (err) throw err
})
Routevoorvoegsels
fastify.register(
require('./route'),
{ prefix: '/v1' }
)
Voorvoegsels worden automatisch toegevoegd aan alle routes die binnen die plugincontext worden verklaard.
fastify-plugin hulpprogramma
const fp = require('fastify-plugin')
module.exports = fp((fastify, opts, next) => {
fastify.decorate('utility', () => {})
next()
}, '0.x')
Stelt plugins in staat om bereikinkapseling te omzeilen en decorators te delen met bovenliggende scopes.
Middleware & Sjablonen
Middleware-integratie
fastify.use(require('cors')())
fastify.use(require('dns-prefetch-control')())
fastify.use(require('frameguard')())
fastify.use(require('hide-powered-by')())
fastify.use(require('hsts')())
fastify.use(require('ienoopen')())
fastify.use(require('x-xss-protection')())