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.
Strings Responses
We can send a string response with the reply.send
method:
const fastify = require('fastify')({})
fastify.get('/', function(req, reply) {
reply.send('plain string')
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Stream Response
We can send a stream response with reply.send
.
For instance, we can write:
index.js
const fastify = require('fastify')({})
fastify.get('/', function(req, reply) {
const fs = require('fs')
const stream = fs.createReadStream('some-file', 'utf8')
reply.send(stream)
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
some-file
foo
Then we see foo
as our response when we go to the /
route.
Buffer Response
We can send a buffer response with the fs.readFile
method.
For example, we can write:
const fastify = require('fastify')({})
fastify.get('/', function(req, reply) {
fs.readFile('some-file', (err, fileBuffer) => {
reply.send(err || fileBuffer)
})
})
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 buffer response in the fs.readFile
callback.
Errors
We can send errors with the http-errors
library.
For instance, we can write:
const fastify = require('fastify')({})
const httpErrors = require('http-errors')
fastify.get('/', function(req, reply) {
reply.send(httpErrors.Gone())
})
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 410 HTTP gone status code.
We can also add an error handler to check for the type of error raised and then send a response.
For instance, we can write:
const fastify = require('fastify')({})
const httpErrors = require('http-errors')
fastify.setErrorHandler(function (error, request, reply) {
request.log.warn(error)
const statusCode = error.statusCode >= 400 ? error.statusCode : 500
reply
.code(statusCode)
.type('text/plain')
.send(statusCode >= 500 ? 'Internal server error' : error.message)
})
fastify.get('/', function(req, reply) {
reply.send(httpErrors.Gone())
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
We get the error status code with the error.statusCode
property.
Then we call reply.code
to set the status code.
type
sets the content type. send
sends the response with the content of the argument.
We can also write:
const fastify = require('fastify')({})
const httpErrors = require('http-errors')
fastify.setNotFoundHandler(function (request, reply) {
reply
.code(404)
.type('text/plain')
.send('a custom 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()
to send the response.
We call callNotFound
to call the not found handler.
Conclusion
We can send various kinds of HTTP responses with Fastify.