Categories
Fastify

Server-Side Development with Fastify — Changing and Sending Responses

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.

Not Found Response

We can call callNotFound to invoke the custom not found handler.

For example, we can write:

const fastify = require('fastify')({})

fastify.setNotFoundHandler({
  preValidation: (req, reply, done) => {
    done()
  },
  preHandler: (req, reply, done) => {
    done()
  }
}, function (request, reply) {
  reply.send('not found')
})

fastify.get('/', function(req, reply) {
  reply.callNotFound()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call setNotFoundHandler to add the not found handler.

Then we call reply.callNotFound in our event handler to redirect to the not found handler.

So we see not found as the response of the / route.

Response Time

We can get the response time with the getResponseTime method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  const milliseconds = reply.getResponseTime()
  reply.send(milliseconds)
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call it in our handler to get the response time.

Response Content-Type

We can set the response Content-Type header with the reply.type method.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.type('text/html').send()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

Then we set the Content-Type response header with the reply.type method.

Raw Response

We can send a raw response with the reply.raw property.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.setCookie('session', 'value', { secure: false })
  reply.raw.writeHead(200, { 'Content-Type': 'text/plain' })
  reply.raw.write('ok')
  reply.raw.end()
})

const start = async () => {
  try {
    await fastify.listen(3000, '0.0.0.0')
  } catch (err) {
    fastify.log.error(err)
    process.exit(1)
  }
}
start()

We call setCookie to set our response cookie.

reply.raw.writeHeader adds the response header.

reply.raw.write writes the response body.

reply.raw.end ends the response.

Set if a Response is Sent

We can set the sent property of the response.

For example, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, reply) {
  reply.sent = true
  reply.raw.end('hello world')
  return Promise.resolve('this will be skipped')
})

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 reply.sent to true to mark the response as sent.

Send a Response

We can send the response with the reply.send method.

For instance, we can write:

const fastify = require('fastify')({})

fastify.get('/', function(req, 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 send the response.

Conclusion

We can set various parts of a response and send it with Fastify.

Leave a Reply

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