Documentation
Factory
The Fastify module exports a factory function that is used to create new Fastify server
instances. This factory function accepts an options object which is used to customize the resulting instance. This document describes the properties available in that options object.
http2
(Status: experimental)
If true
Node.js core's HTTP/2 module is used for binding the socket.
- Default:
false
https
An object used to configure the server's listening socket for TLS. The options are the same as the Node.js core createServer
method. When this property is null
, the socket will not be configured for TLS.
This option also applies when the http2
option is set.
- Default:
null
ignoreTrailingSlash
Fastify uses find-my-way to handle routing. This option may be set to true
to ignore trailing slashes in routes. This option applies to all route registrations for the resulting server instance.
- Default:
false
const fastify = require('fastify')({
ignoreTrailingSlash: true
})
// registers both "/foo" and "/foo/"
fastify.get('/foo/', function (req, reply) {
res.send('foo')
})
// registers both "/bar" and "/bar/"
fastify.get('/bar', function (req, reply) {
res.send('bar')
})
maxParamLength
You can set a custom length for parameters in parametric (standard, regex and multi) routes by using maxParamLength
option, the default value is 100 characters.
This can be useful especially if you have some regex based route, protecting you against DoS attacks.
If the maximum length limit is reached, the not found route will be invoked.
bodyLimit
Defines the maximum payload, in bytes, the server is allowed to accept.
- Default:
1048576
(1MiB)
logger
Fastify includes built-in logging via the Pino logger. This property is used to configure the internal logger instance.
The possible values this property may have are:
Default:
false
. The logger is disabled. All logging methods will point to a null logger abstract-logging instance.pinoInstance
: a previously instantiated instance of Pino. The internal logger will point to this instance.object
: a standard Pino options object. This will be passed directly to the Pino constructor. If the following properties are not present on the object, they will be added accordingly:genReqId
: a synchronous function that will be used to generate identifiers for incoming requests. The default function generates sequential identifiers.level
: the minimum logging level. If not set, it will be set to'info'
.serializers
: a hash of serialization functions. By default, serializers are added forreq
(incoming request objects),res
(outgoing repsonse objets), anderr
(standardError
objects). When a log method receives an object with any of these properties then the respective serializer will be used for that property. For example:
Any user supplied serializer will override the default serializer of the corresponding property.fastify.get('/foo', function (req, res) { req.log.info({req}) // log the serialized request object res.send('foo') })
serverFactory
You can pass a custom http server to Fastify by using the serverFactory
option.serverFactory
is a function that takes an handler
parameter, which takes the request
and response
objects as parameters, and an options object, which is the same you have passed to Fastify.
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' })
})
fastify.listen(3000)
Internally Fastify uses the API of Node core http server, so if you are using a custom server you must be sure to have the same API exposed. If not, you can enhance the server instance inside the serverFactory
function before the return
statement.
caseSensitive
By default, value equal to true
, routes are registered as case sensitive. That is, /foo
is not equivalent to /Foo
. When set to false
, routes are registered in a fashion such that /foo
is equivalent to /Foo
which is equivalent to /FOO
.
Setting caseSensitive
to false
will also result in all params (and all value matched by regexps) to be lowercased as well.
fastify.get('/user/:username', (request, reply) => {
// Given the URL: /user/NodeJS
console.log(request.params.username) // -> 'nodejs'
})
Please note this setting this option to false
goes against RFC3986.
requestIdHeader
The header name used to know the request id. See the request id section.
- Default:
'request-id'