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.
Server Options
We can set some options when we require fastify
.
http2
lets us use HTTP/2 to bind the socket.
https
lets ys listen for TLS connections.
connectionTimeout
lets us define the server timeout in milliseconds.
keepAliveTimeout
lets us define the server keep-alive timeout in milliseconds.
ignoreTrailingSlash
makes Fastify ignore trailing slashes when matching routes.
For example, if we have:
const fastify = require('fastify')({
ignoreTrailingSlash: true
})
fastify.get('/foo/', function(req, reply) {
reply.send('foo')
})
// registers both "/bar" and "/bar/"
fastify.get('/bar', function(req, reply) {
reply.send('bar')
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
The /foo
and /foo/
matches the /foo/
route.
And /bar
and /bar/
matches the /bar/
route.
maxParamLength
sets the max length of the route parameter. Anything longer than the length won’t be invoked.
bodyLimit
sets the max request body size.
logger
lets us enable the logger.
We can add pino
as a custom logger by writing:
const pino = require('pino')();
const customLogger = {
info(o, ...n) { },
warn(o, ...n) { },
error(o, ...n) { },
fatal(o, ...n) { },
trace(o, ...n) { },
debug(o, ...n) { },
child() {
const child = Object.create(this);
child.pino = pino.child(...arguments);
return child;
},
};
const fastify = require('fastify')({
logger: customLogger
})
fastify.get('/foo/', function(req, reply) {
reply.send('foo')
})
// registers both "/bar" and "/bar/"
fastify.get('/bar', function(req, reply) {
reply.send('bar')
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
disableRequestLogging
lets us disable logging of requests.
serverFactory
lets us pass a custom HTTP server to Fastify.
For example, we can write:
const Fastify = require('fastify')
const http = require('http')
const serverFactory = (handler, opts) => {
const server = http.createServer((req, res) => {
handler(req, res)
})
return server
}
const fastify = Fastify({ serverFactory })
fastify.get('/', (req, reply) => {
reply.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 create a server with the http
module.
caseSensitive
lets us set whether to match routes in a case sensitive manner.
For example, if we have:
const fastify = require('fastify')({
caseSensitive: false
})
fastify.get('/user/:username', (request, reply) => {
reply.send(request.params.username)
})
const start = async () => {
try {
await fastify.listen(3000, '0.0.0.0')
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Then if we go to /USER/NODEJS
, we see NODEJS
since caseSensitive
is false
.
The genReqId
option lets us generate the request ID with our own function,.
For example, we can write:
let i = 0
const fastify = require('fastify')({
genReqId (req) { return i++ }
})
fastify.get('/', (request, reply) => {
reply.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 create our own function to generate the request ID.
Conclusion
We can configure a few options with our Fastify server.