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 stringbody
– the bodyparams
– the params matching the URLheaders
– the headersraw
– the incoming HTTP request from Node coreid
– the request IDlog
– the logger instance of the incoming requestip
– the IP address of the incoming requestips
– an array of the IP addresses in theX-Forwarded-For
header of the incoming request. It’s available only when thetrustProxy
option is enabledhostname
– the hostname of the incoming requestprotocol
– the protocol of the incoming request (https
orhttp
)method
– the method of the incoming requesturl
– the URL of the incoming requestrouterMethod
– the method defined for the router that is handling the requestrouterPath
– the path pattern defined for the router that is handling the requestis404
–true
if the request is being handled by 404 handlers,false
if it is notsocket
– 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.