Member-only story
Server-Side Development with Fastify — Sending Responses
3 min readFeb 24, 2021
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