Categories
Fastify

Server-Side Development with Fastify — 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.

Reply Object

Then reply object lets us send HTTP responses to the client.

We can set the status code with the code method.

For instance, we can write:

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

fastify.get('/', function(req, reply) {
  reply
    .code(200)
    .header('Content-Type', 'application/json; charset=utf-8')
    .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 a 200 response with the code method.

We can check the status code with the statusCode method.

For example, we can write:

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

fastify.get('/', function(req, reply) {
  if (reply.statusCode >= 299) {
    reply.statusCode = 500
  }
  reply.send()
})

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 statusCode to 500.

Response Headers

We can set the headers of the response with the reply.headers method.

For instance, we can write:

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

fastify.get('/', function(req, reply) {
  reply.headers({
    'x-foo': 'foo',
    'x-bar': 'bar'
  })
  .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 send the x-foo and x-bar headers.

We can get the header with the getHeader method.

For instance, we can write:

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

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  console.log(reply.getHeader('x-foo'))
  reply.send()
})

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

We call getHeader to get the header.

Also, we can get all the headers with the getHeaders method:

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

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  reply.header('x-bar', 'bar')
  reply.raw.setHeader('x-foo', 'foo2')
  console.log(reply.getHeaders())
  reply.send()
})

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

To remove a header, we call removeHeader to do so:

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

fastify.get('/', function(req, reply) {
  reply.header('x-foo', 'foo')
  reply.removeHeader('x-foo')
  console.log(reply.getHeader('x-foo'))
  reply.send()
})

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

We called removeHeader to remove the x-foo header, so when we call getHeader on it, we get undefined .

Redirect

We can redirect to a different route with the redirect method.

For example, we can write:

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

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

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

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

Then we redirect to the /home route.

Also, we can add the status code by writing:

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

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

fastify.get('/', function(req, reply) {
  reply.code(303).redirect('/home')
})

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

or:

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

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

fastify.get('/', function(req, reply) {
  reply.redirect(303, '/home')
})

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

Conclusion

We can send responses to the client with Fastify.

Leave a Reply

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