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.
Decorator Getters and Setters
We can add getters and setters to decorators.
For example, we can write:
const fastify = require('fastify')({})
fastify.decorate('foo', {
getter () {
return 'a getter'
}
})
fastify.get('/', async function (request, reply) {
reply.send({hello: fastify.foo})
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Then fastify.foo returns 'a getter' .
So we get:
{"hello":"a getter"}
as the response of the / route.
Validation
We can add validation for requests.
For example, we can write:
const fastify = require('fastify')({})
fastify.addSchema({
$id: 'http://example.com/',
type: 'object',
properties: {
hello: { type: 'string' }
}
})
fastify.post('/', {
handler (request, reply) {
reply.send('hello')
},
schema: {
body: {
type: 'array',
items: { $ref: 'http://example.com#/properties/hello' }
}
}
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We call fastify.addSchema to add the schema.
We specify that it is a string with the properties property.
Then we reference it in the object in the body.
body specifies the body structure.
We have the items property with the $ref property that references the properties.hello property to get the data type of the item of the array.
So when we make a POST request to the / route with a body like:
[
"abc"
]
we see 'hello' in the response.
Otherwise, we get a 400 error.
$ref can also be used as a root reference.
For example, we can write:
const fastify = require('fastify')({})
fastify.addSchema({
$id: 'commonSchema',
type: 'object',
properties: {
hello: { type: 'string' }
}
})
fastify.post('/', {
handler (request, reply) {
reply.send('hello')
},
schema: {
body: { $ref: 'commonSchema#' },
headers: { $ref: 'commonSchema#' }
}
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Then when we send an object with the request body, we, get the 'hello' response.
Retrieving Shared Schemas
We can get a shared schema.
For example, we can write:
const fastify = require('fastify')({})
fastify.addSchema({
$id: 'schemaId',
type: 'object',
properties: {
hello: { type: 'string' }
}
})
const mySchemas = fastify.getSchemas()
const mySchema = fastify.getSchema('schemaId')
console.log(mySchemas, mySchema)
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We call addSchema to add our schema.
Then we can get the schemas with the getSchemas method.
The getSchema method gets the schema with the given ID.
We can also use that within the select scope.
For instance, we can write:
const fastify = require('fastify')({})
fastify.addSchema({ $id: 'one', my: 'hello' })
fastify.get('/', (request, reply) => { reply.send(fastify.getSchemas()) })
fastify.register((instance, opts, done) => {
instance.addSchema({ $id: 'two', my: 'ciao' })
instance.get('/sub', (request, reply) => { reply.send(instance.getSchemas()) })
instance.register((subinstance, opts, done) => {
subinstance.addSchema({ $id: 'three', my: 'hola' })
subinstance.get('/deep', (request, reply) => { reply.send(subinstance.getSchemas()) })
done()
})
done()
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Then we call addSchema to add the schema inside the given context.
It’ll only be available within the given context.
getSchemas also get the schemas within the given context.
So when we make a request to the / route, we get:
{"one":{"$id":"one","my":"hello"}}
as the response.
Conclusion
We can add decorators with getters and setters, and we can add request validation schemas with Fastify.