Member-only story

Server-Side Development with Fastify — Decorator Getters and Setters and Validation

John Au-Yeung
3 min readFeb 19, 2021

--

Photo by Shiro hatori on Unsplash

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'…

--

--

No responses yet