Fastify is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Fastify.
Initial Config
We can set the initial config of the app with we initialize Fastify.
To do this, we write:
const { readFileSync } = require('fs')
const Fastify = require('fastify')
const fastify = Fastify({
https: {
allowHTTP1: true,
key: readFileSync('./fastify.key'),
cert: readFileSync('./fastify.cert')
},
logger: { level: 'trace'},
ignoreTrailingSlash: true,
maxParamLength: 200,
caseSensitive: true,
trustProxy: '127.0.0.1,192.168.1.1/24',
})
console.log(fastify.initialConfig)
fastify.get('/', async (request, reply) => {
return 'hello world'
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to set the initial config.
https
has HTTPS options.
allowHTTP1
lets us listen for HTTP1 requests.
key
and cert
are the SSL key and certificate files.
logger
sets the logging level.
ignoreTrailingSlash
lets Fastify ignore trailing slashes when matching routes.
caseSensitive
lets us set whether the enable case sensitive route matching.
trustProxy
lets us set which proxy to trust.
We get the config with the fastify.initialConfig
property.
Also, we can set the options on the fly with:
const { readFileSync } = require('fs')
const Fastify = require('fastify')
const fastify = Fastify({
https: {
allowHTTP1: true,
key: readFileSync('./fastify.key'),
cert: readFileSync('./fastify.cert')
},
logger: { level: 'trace'},
ignoreTrailingSlash: true,
maxParamLength: 200,
caseSensitive: true,
trustProxy: '127.0.0.1,192.168.1.1/24',
})
console.log(fastify.initialConfig)
fastify.register(async (instance, opts) => {
instance.get('/', async (request, reply) => {
return instance.initialConfig
})
instance.get('/error', async (request, reply) => {
instance.initialConfig.https.allowHTTP1 = false
return instance.initialConfig
})
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We write instance.initialConfig.https.allowHTTP1 = false
to set the allowHTTP1
option in the /error
route handler.
Properties that can be exposed include:
connectionTimeout
keepAliveTimeout
bodyLimit
caseSensitive
http2
https
(it will returnfalse
/true
or{ allowHTTP1: true/false }
if explicitly passed)ignoreTrailingSlash
maxParamLength
onProtoPoisoning
pluginTimeout
requestIdHeader
Routes
We can declare routes with the fastify.route
method.
For instance, we can write:
const fastify = require('fastify')()
fastify.route({
method: 'GET',
url: '/',
schema: {
querystring: {
name: { type: 'string' },
excitement: { type: 'integer' }
},
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
},
handler: function (request, reply) {
reply.send({ hello: 'world' })
}
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
to add a /
route.
We specify the query string that it accepts with the querystring
property.
And we specify the response that it returns with the response
property.
The handler
has the route handler function.
reply.send
sends the response to the user.
Conclusion
We can set the initial config of our Fastify app and add routes to our Fastify app with fastify.route
.