Categories
Fastify

Server-Side Development with Fastify — Versioning and Logging

Spread the love

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.

Version

We can set the version to accept with the version property.

For instance, we can write:

const fastify = require('fastify')()

fastify.route({
  method: 'GET',
  url: '/',
  version: '1.2.0',
  handler (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()

Then our / route will check the Accept-Version header from the request to see if it matches what we have.

If we make a request with Accept-Version set to 1.x , 1.2.0 or 1.2.x , then we see:

{ hello: 'world' }

in the response.

Otherwise, we get 404.

Logging

Logging is disabled by default.

We can enable logging by setting the logger property.

For instance, we can write:

const fastify = require('fastify')({ logger: true })

fastify.get('/', (request, reply) => {
  request.log.info('Some info')
  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 enable logging with logger set to true .

Then we call request.log.info to log something to the console.

We should see something like.

{"level":30,"time":1604268389762,"pid":737,"hostname":"9cc07d2eed9d","reqId":2,"msg":"Some info"}

logged.

We can also pass some options to the logger.

For example, we can write:

const fastify = require('fastify')({
  logger: {
    level: 'info',
    file: './log.txt'
  }
})

fastify.get('/', (request, reply) => {
  request.log.info('Some info')
  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()

Then we log the output to log.txt .

We can pass a custom stream to the Pino instance.

To do this, we write:

const split = require('split2')
const stream = split(JSON.parse)

const fastify = require('fastify')({
  logger: {
    level: 'info',
    stream: stream
  }
})
fastify.get('/', (request, reply) => {
  request.log.info('Some info')
  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 pass in a custom stream that’s created with:

const stream = split(JSON.parse)

Also, we can customize what’s logged with the serializers property.

We can write:

const fastify = require('fastify')({
  logger: {
    serializers: {
      req (request) {
        return { url: request.url }
      }
    }
  }
})

fastify.get('/', (request, reply) => {
  request.log.info('Some info')
  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 the serializers property which has the req function to return an object with the data we want to record from the request.

Conclusion

We can set the version of the API and adding logging with Fastify.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *