Categories
Fastify

Server-Side Development with Fastify — Async and Await and Requests

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.

Async-Await and Promises

We can use async functions with our route handlers and send responses with it.

For example, we can write:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  await delay(200)
  return { hello: 'world' }
})

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

We use an async function as our route handler, so we can await promises.

Then we just return the response body to send the response.

We can throw responses with an Error instance.

For instance, we can write:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  const err = new Error()
  err.statusCode = 418
  err.message = 'short and stout'
  throw err
})

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

We can also throw an object by writing:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.get('/', async function (request, reply) {
  throw { statusCode: 418, message: 'short and stout' }
})

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

Request

The request object has various properties.

They include:

  • query – the parsed query string
  • body – the body
  • params – the params matching the URL
  • headers – the headers
  • raw – the incoming HTTP request from Node core
  • id – the request ID
  • log – the logger instance of the incoming request
  • ip – the IP address of the incoming request
  • ips – an array of the IP addresses in the X-Forwarded-For header of the incoming request. It’s available only when the trustProxy option is enabled
  • hostname – the hostname of the incoming request
  • protocol – the protocol of the incoming request (https or http)
  • method – the method of the incoming request
  • url – the URL of the incoming request
  • routerMethod – the method defined for the router that is handling the request
  • routerPath – the path pattern defined for the router that is handling the request
  • is404true if the request is being handled by 404 handlers, false if it is not
  • socket – the underlying connection of the incoming request

We can access them by sitting:

const fastify = require('fastify')({})
const { promisify } = require('util');
const delay = promisify(setTimeout)

fastify.post('/:params', options, function (request, reply) {
  console.log(request.body)
  console.log(request.query)
  console.log(request.params)
  console.log(request.headers)
  console.log(request.raw)
  console.log(request.id)
  console.log(request.ip)
  console.log(request.ips)
  console.log(request.hostname)
  console.log(request.protocol)
  request.log.info('some info')
})

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 and get request data with Fastify.

Leave a Reply

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