Fastify är ett högeffektivt webbramverk med minimal overhead, fokuserat på att leverera fantastisk utvecklarupplevelse och maximal genomströmning.
Kom igång
Introduktion och Dokumentation
Fastify levererar enastående prestanda genom schemabaserad kompilering och en modulär plugin-arkitektur.
- Fastify Source Code (github.com)
- Fastify Documentation (fastify.dev)
Hello World-exempel
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
Ruttdeklaration
fastify.route({
method: 'GET',
url: '/',
schema: { ··· },
handler: (req, reply) => { ··· },
beforeHandler: (req, reply, done) => { ··· }
})
Kortkommandometoder (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 Hanterare
fastify.get('/', options, async (req, reply) => {
return data
// or
reply.send(data)
})
Med asynkrona hanterare kan du helt enkelt returnera data eller anropa reply.send(data).
Förfrågan och Svar (Request & Reply)
Request-objekt
request.query
request.body
request.params
request.headers
request.req // Node.js core incoming request
request.log.info('hello')
See: Request.
Reply-objekt
Svars-headers
reply.code(404)
reply.header('Content-Type', 'text/html')
reply.type('text/html')
Omdirigeringar
reply.redirect('/foo')
reply.redirect(302, '/foo')
Skicka data
reply.send(payload)
reply.sent // → true|false
See: Reply.
JSON-schemavalidering
Definiera ett schema
const schema = {
querystring: {
name: { type: 'string' },
excitement: { type: 'integer' }
},
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
}
Skicka schema till rutt
fastify.get('/', { schema }, (req, reply) => {
···
})
Rutt-alternativobjekt
fastify.route({
method: 'GET',
url: '/',
schema,
handler: (req, reply) => { ··· }
})
Att definiera ett JSON-schema möjliggör blixtsnabb validering och automatisk serialiseringsoptimering.
See: Validation and Serialization.
Plugin-arkitektur
Registrera funktion som plugin
fastify.register(
require('./route'),
err => { if (err) throw err }
)
route.js
module.exports = (fastify, options, next) => {
fastify.get('/', (req, reply) => { ··· })
next()
}
See: Plugins.
Registrera flera plugins
fastify.register([
require('./another-route'),
require('./yet-another-route')
], opts, (err) => {
if (err) throw err
})
Ruttprefix
fastify.register(
require('./route'),
{ prefix: '/v1' }
)
Prefix läggs automatiskt till alla rutter som deklareras i den plugin-kontexten.
Verktyget fastify-plugin
const fp = require('fastify-plugin')
module.exports = fp((fastify, opts, next) => {
fastify.decorate('utility', () => {})
next()
}, '0.x')
Tillåter plugins att kringgå omfångskapsling och dela dekoratörer med överordnade omfång.
Middleware & Mallar
Middleware-integration
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')())