Server

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.

http

  • Default: null

An object used to configure the server’s listening socket. The options are the same as the Node.js core createServer method.

This option is ignored if options http2 or https are set.

http2

  • Default: false

If true Node.js core’s HTTP/2 module is used for binding the socket.

https

  • Default: null

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.

connectionTimeout

  • Default: 0 (no timeout)

Defines the server timeout in milliseconds. See documentation for server.timeout property to understand the effect of this option.

When serverFactory option is specified this option is ignored.

keepAliveTimeout

  • Default: 72000 (72 seconds)

Defines the server keep-alive timeout in milliseconds. See documentation for server.keepAliveTimeout property to understand the effect of this option. This option only applies when HTTP/1 is in use.

When serverFactory option is specified this option is ignored.

forceCloseConnections

  • Default: "idle" if the HTTP server allows it, false otherwise

When set to true, upon close the server will iterate the current persistent connections and destroy their sockets.

Warning Connections are not inspected to determine if requests have been completed.

Fastify will prefer the HTTP server’s closeAllConnections method if supported, otherwise, it will use internal connection tracking.

When set to "idle", upon close the server will iterate the current persistent connections which are not sending a request or waiting for a response and destroy their sockets. The value is only supported if the HTTP server supports the closeIdleConnections method, otherwise attempting to set it will throw an exception.

maxRequestsPerSocket

  • Default: 0 (no limit)

Defines the maximum number of requests a socket can handle before closing keep alive connection. See server.maxRequestsPerSocket property to understand the effect of this option. This option only applies when HTTP/1.1 is in use. Also, when serverFactory option is specified, this option is ignored.

Note At the time of writing, only node >= v16.10.0 supports this option.

requestTimeout

  • Default: 0 (no limit)

Defines the maximum number of milliseconds for receiving the entire request from the client. See server.requestTimeout property to understand the effect of this option.

When serverFactory option is specified, this option is ignored. It must be set to a non-zero value (e.g. 120 seconds) to protect against potential Denial-of-Service attacks in case the server is deployed without a reverse proxy in front.

Note At the time of writing, only node >= v14.11.0 supports this option

ignoreTrailingSlash

  • Default: false

Fastify uses find-my-way to handle routing. By default, Fastify will take into account the trailing slashes. Paths like /foo and /foo/ are treated as different paths. If you want to change this, set this flag to true. That way, both /foo and /foo/ will point to the same route. This option applies to all route registrations for the resulting server instance.

  1. const fastify = require('fastify')({
  2. ignoreTrailingSlash: true
  3. })
  4. // registers both "/foo" and "/foo/"
  5. fastify.get('/foo/', function (req, reply) {
  6. reply.send('foo')
  7. })
  8. // registers both "/bar" and "/bar/"
  9. fastify.get('/bar', function (req, reply) {
  10. reply.send('bar')
  11. })

ignoreDuplicateSlashes

  • Default: false

Fastify uses find-my-way to handle routing. You can use ignoreDuplicateSlashes option to remove duplicate slashes from the path. It removes duplicate slashes in the route path and the request URL. This option applies to all route registrations for the resulting server instance.

When ignoreTrailingSlash and ignoreDuplicateSlashes are both set to true Fastify will remove duplicate slashes, and then trailing slashes, meaning //a//b//c// will be converted to /a/b/c.

  1. const fastify = require('fastify')({
  2. ignoreDuplicateSlashes: true
  3. })
  4. // registers "/foo/bar/"
  5. fastify.get('///foo//bar//', function (req, reply) {
  6. reply.send('foo')
  7. })

maxParamLength

  • Default: 100

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. If the maximum length limit is reached, the not found route will be invoked.

This can be useful especially if you have a regex-based route, protecting you against ReDoS attacks.

bodyLimit

  • Default: 1048576 (1MiB)

Defines the maximum payload, in bytes, the server is allowed to accept. The default body reader sends FST_ERR_CTP_BODY_TOO_LARGE reply, if the size of the body exceeds this limit. If preParsing hook is provided, this limit is applied to the size of the stream the hook returns (i.e. the size of “decoded” body).

onProtoPoisoning

  • Default: 'error'

Defines what action the framework must take when parsing a JSON object with __proto__. This functionality is provided by secure-json-parse. See Prototype Poisoning for more details about prototype poisoning attacks.

Possible values are 'error', 'remove', or 'ignore'.

onConstructorPoisoning

  • Default: 'error'

Defines what action the framework must take when parsing a JSON object with constructor. This functionality is provided by secure-json-parse. See Prototype Poisoning for more details about prototype poisoning attacks.

Possible values are 'error', 'remove', or 'ignore'.

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.

  • 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:

    • 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 for req (incoming request objects), res (outgoing response objects), and err (standard Error 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:

      1. fastify.get('/foo', function (req, res) {
      2. req.log.info({req}) // log the serialized request object
      3. res.send('foo')
      4. })

      Any user-supplied serializer will override the default serializer of the corresponding property.

loggerInstance

  • Default: null

A custom logger instance. The logger must be a Pino instance or conform to the Pino interface by having the following methods: info, error, debug, fatal, warn, trace, child. For example:

  1. const pino = require('pino')();
  2. const customLogger = {
  3. info: function (o, ...n) {},
  4. warn: function (o, ...n) {},
  5. error: function (o, ...n) {},
  6. fatal: function (o, ...n) {},
  7. trace: function (o, ...n) {},
  8. debug: function (o, ...n) {},
  9. child: function() {
  10. const child = Object.create(this);
  11. child.pino = pino.child(...arguments);
  12. return child;
  13. },
  14. };
  15. const fastify = require('fastify')({logger: customLogger});

disableRequestLogging

  • Default: false

When logging is enabled, Fastify will issue an info level log message when a request is received and when the response for that request has been sent. By setting this option to true, these log messages will be disabled. This allows for more flexible request start and end logging by attaching custom onRequest and onResponse hooks.

The other log entries that will be disabled are:

  • an error log written by the default onResponse hook on reply callback errors
  • the error and info logs written by the defaultErrorHandler on error management
  • the info log written by the fourOhFour handler when a non existent route is requested

Other log messages emitted by Fastify will stay enabled, like deprecation warnings and messages emitted when requests are received while the server is closing.

  1. // Examples of hooks to replicate the disabled functionality.
  2. fastify.addHook('onRequest', (req, reply, done) => {
  3. req.log.info({ url: req.raw.url, id: req.id }, 'received request')
  4. done()
  5. })
  6. fastify.addHook('onResponse', (req, reply, done) => {
  7. req.log.info({ url: req.raw.originalUrl, statusCode: reply.raw.statusCode }, 'request completed')
  8. done()
  9. })

serverFactory

You can pass a custom HTTP server to Fastify by using the serverFactory option.

serverFactory is a function that takes a handler parameter, which takes the request and response objects as parameters, and an options object, which is the same you have passed to Fastify.

  1. const serverFactory = (handler, opts) => {
  2. const server = http.createServer((req, res) => {
  3. handler(req, res)
  4. })
  5. return server
  6. }
  7. const fastify = Fastify({ serverFactory })
  8. fastify.get('/', (req, reply) => {
  9. reply.send({ hello: 'world' })
  10. })
  11. fastify.listen({ port: 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

  • Default: true

When true routes are registered as case-sensitive. That is, /foo is not equal to /Foo. When false then routes are case-insensitive.

Please note that setting this option to false goes against RFC3986.

By setting caseSensitive to false, all paths will be matched as lowercase, but the route parameters or wildcards will maintain their original letter casing. This option does not affect query strings, please refer to querystringParser to change their handling.

  1. fastify.get('/user/:username', (request, reply) => {
  2. // Given the URL: /USER/NodeJS
  3. console.log(request.params.username) // -> 'NodeJS'
  4. })

allowUnsafeRegex

  • Default false

Disabled by default, so routes only allow safe regular expressions. To use unsafe expressions, set allowUnsafeRegex to true.

  1. fastify.get('/user/:id(^([0-9]+){4}$)', (request, reply) => {
  2. // Throws an error without allowUnsafeRegex = true
  3. })

requestIdHeader

  • Default: 'request-id'

The header name used to set the request-id. See the request-id section. Setting requestIdHeader to true will set the requestIdHeader to "request-id". Setting requestIdHeader to a non-empty string will use the specified string as the requestIdHeader. By default requestIdHeader is set to false and will immediately use genReqId. Setting requestIdHeader to an empty String ("") will set the requestIdHeader to false.

  • Default: false
  1. const fastify = require('fastify')({
  2. requestIdHeader: 'x-custom-id', // -> use 'X-Custom-Id' header if available
  3. //requestIdHeader: false, // -> always use genReqId
  4. })

requestIdLogLabel

  • Default: 'reqId'

Defines the label used for the request identifier when logging the request.

genReqId

  • Default: value of 'request-id' header if provided or monotonically increasing integers

Function for generating the request-id. It will receive the raw incoming request as a parameter. This function is expected to be error-free.

Especially in distributed systems, you may want to override the default ID generation behavior as shown below. For generating UUIDs you may want to check out hyperid.

Note genReqId will be not called if the header set in [requestIdHeader](#requestidheader) is available (defaults to ‘request-id’).

  1. let i = 0
  2. const fastify = require('fastify')({
  3. genReqId: function (req) { return i++ }
  4. })

trustProxy

  • Default: false
  • true/false: Trust all proxies (true) or do not trust any proxies (false).
  • string: Trust only given IP/CIDR (e.g. '127.0.0.1'). May be a list of comma separated values (e.g. '127.0.0.1,192.168.1.1/24').
  • Array<string>: Trust only given IP/CIDR list (e.g. ['127.0.0.1']).
  • number: Trust the nth hop from the front-facing proxy server as the client.
  • Function: Custom trust function that takes address as first argument

    1. function myTrustFn(address, hop) {
    2. return address === '1.2.3.4' || hop === 1
    3. }

By enabling the trustProxy option, Fastify will know that it is sitting behind a proxy and that the X-Forwarded-* header fields may be trusted, which otherwise may be easily spoofed.

  1. const fastify = Fastify({ trustProxy: true })

For more examples, refer to the proxy-addr package.

You may access the ip, ips, host and protocol values on the request object.

  1. fastify.get('/', (request, reply) => {
  2. console.log(request.ip)
  3. console.log(request.ips)
  4. console.log(request.host)
  5. console.log(request.protocol)
  6. })

Note If a request contains multiple x-forwarded-host or x-forwarded-proto headers, it is only the last one that is used to derive request.hostname and request.protocol.

pluginTimeout

  • Default: 10000

The maximum amount of time in milliseconds in which a plugin can load. If not, ready will complete with an Error with code 'ERR_AVVIO_PLUGIN_TIMEOUT'. When set to 0, disables this check. This controls avvio ‘s timeout parameter.

querystringParser

The default query string parser that Fastify uses is the Node.js’s core querystring module.

You can use this option to use a custom parser, such as qs.

If you only want the keys (and not the values) to be case insensitive we recommend using a custom parser to convert only the keys to lowercase.

  1. const qs = require('qs')
  2. const fastify = require('fastify')({
  3. querystringParser: str => qs.parse(str)
  4. })

You can also use Fastify’s default parser but change some handling behavior, like the example below for case insensitive keys and values:

  1. const querystring = require('node:querystring')
  2. const fastify = require('fastify')({
  3. querystringParser: str => querystring.parse(str.toLowerCase())
  4. })

exposeHeadRoutes

  • Default: true

Automatically creates a sibling HEAD route for each GET route defined. If you want a custom HEAD handler without disabling this option, make sure to define it before the GET route.

constraints

Fastify’s built-in route constraints are provided by find-my-way, which allows constraining routes by version or host. You can add new constraint strategies, or override the built-in strategies, by providing a constraints object with strategies for find-my-way. You can find more information on constraint strategies in the find-my-way documentation.

  1. const customVersionStrategy = {
  2. storage: function () {
  3. const versions = {}
  4. return {
  5. get: (version) => { return versions[version] || null },
  6. set: (version, store) => { versions[version] = store }
  7. }
  8. },
  9. deriveVersion: (req, ctx) => {
  10. return req.headers['accept']
  11. }
  12. }
  13. const fastify = require('fastify')({
  14. constraints: {
  15. version: customVersionStrategy
  16. }
  17. })

return503OnClosing

  • Default: true

Returns 503 after calling close server method. If false, the server routes the incoming request as usual.

ajv

Configure the Ajv v8 instance used by Fastify without providing a custom one. The default configuration is explained in the #schema-validator section.

  1. const fastify = require('fastify')({
  2. ajv: {
  3. customOptions: {
  4. removeAdditional: 'all' // Refer to [ajv options](https://ajv.js.org/options.html#removeadditional)
  5. },
  6. plugins: [
  7. require('ajv-merge-patch'),
  8. [require('ajv-keywords'), 'instanceof']
  9. // Usage: [plugin, pluginOptions] - Plugin with options
  10. // Usage: plugin - Plugin without options
  11. ]
  12. }
  13. })

serializerOpts

Customize the options of the default fast-json-stringify instance that serializes the response’s payload:

  1. const fastify = require('fastify')({
  2. serializerOpts: {
  3. rounding: 'ceil'
  4. }
  5. })

http2SessionTimeout

  • Default: 72000

Set a default timeout to every incoming HTTP/2 session in milliseconds. The session will be closed on the timeout.

This option is needed to offer a graceful “close” experience when using HTTP/2. The low default has been chosen to mitigate denial of service attacks. When the server is behind a load balancer or can scale automatically this value can be increased to fit the use case. Node core defaults this to 0.

frameworkErrors

  • Default: null

Fastify provides default error handlers for the most common use cases. It is possible to override one or more of those handlers with custom code using this option.

Note Only FST_ERR_BAD_URL and FST_ERR_ASYNC_CONSTRAINT are implemented at present.

  1. const fastify = require('fastify')({
  2. frameworkErrors: function (error, req, res) {
  3. if (error instanceof FST_ERR_BAD_URL) {
  4. res.code(400)
  5. return res.send("Provided url is not valid")
  6. } else if(error instanceof FST_ERR_ASYNC_CONSTRAINT) {
  7. res.code(400)
  8. return res.send("Provided header is not valid")
  9. } else {
  10. res.send(err)
  11. }
  12. }
  13. })

clientErrorHandler

Set a clientErrorHandler that listens to error events emitted by client connections and responds with a 400.

It is possible to override the default clientErrorHandler using this option.

  • Default:
  1. function defaultClientErrorHandler (err, socket) {
  2. if (err.code === 'ECONNRESET') {
  3. return
  4. }
  5. const body = JSON.stringify({
  6. error: http.STATUS_CODES['400'],
  7. message: 'Client Error',
  8. statusCode: 400
  9. })
  10. this.log.trace({ err }, 'client error')
  11. if (socket.writable) {
  12. socket.end([
  13. 'HTTP/1.1 400 Bad Request',
  14. `Content-Length: ${body.length}`,
  15. `Content-Type: application/json\r\n\r\n${body}`
  16. ].join('\r\n'))
  17. }
  18. }

Note clientErrorHandler operates with raw sockets. The handler is expected to return a properly formed HTTP response that includes a status line, HTTP headers and a message body. Before attempting to write the socket, the handler should check if the socket is still writable as it may have already been destroyed.

  1. const fastify = require('fastify')({
  2. clientErrorHandler: function (err, socket) {
  3. const body = JSON.stringify({
  4. error: {
  5. message: 'Client error',
  6. code: '400'
  7. }
  8. })
  9. // `this` is bound to fastify instance
  10. this.log.trace({ err }, 'client error')
  11. // the handler is responsible for generating a valid HTTP response
  12. socket.end([
  13. 'HTTP/1.1 400 Bad Request',
  14. `Content-Length: ${body.length}`,
  15. `Content-Type: application/json\r\n\r\n${body}`
  16. ].join('\r\n'))
  17. }
  18. })

rewriteUrl

Set a sync callback function that must return a string that allows rewriting URLs. This is useful when you are behind a proxy that changes the URL. Rewriting a URL will modify the url property of the req object.

Note that rewriteUrl is called before routing, it is not encapsulated and it is an instance-wide configuration.

  1. // @param {object} req The raw Node.js HTTP request, not the `FastifyRequest` object.
  2. // @this Fastify The root Fastify instance (not an encapsulated instance).
  3. // @returns {string} The path that the request should be mapped to.
  4. function rewriteUrl (req) {
  5. if (req.url === '/hi') {
  6. this.log.debug({ originalUrl: req.url, url: '/hello' }, 'rewrite url');
  7. return '/hello'
  8. } else {
  9. return req.url;
  10. }
  11. }

useSemicolonDelimiter

  • Default false

Fastify uses find-my-way which supports, separating the path and query string with a ; character (code 59), e.g. /dev;foo=bar. This decision originated from [delvedor/find-my-way#76] (https://github.com/delvedor/find-my-way/issues/76). Thus, this option will support backwards compatiblilty for the need to split on ;. To enable support for splitting on ; set useSemicolonDelimiter to true.

  1. const fastify = require('fastify')({
  2. useSemicolonDelimiter: true
  3. })
  4. fastify.get('/dev', async (request, reply) => {
  5. // An example request such as `/dev;foo=bar`
  6. // Will produce the following query params result `{ foo = 'bar' }`
  7. return request.query
  8. })

Instance

Server Methods

server

fastify.server: The Node core server object as returned by the Fastify factory function.

Warning If utilized improperly, certain Fastify features could be disrupted. It is recommended to only use it for attaching listeners.

after

Invoked when the current plugin and all the plugins that have been registered within it have finished loading. It is always executed before the method fastify.ready.

  1. fastify
  2. .register((instance, opts, done) => {
  3. console.log('Current plugin')
  4. done()
  5. })
  6. .after(err => {
  7. console.log('After current plugin')
  8. })
  9. .register((instance, opts, done) => {
  10. console.log('Next plugin')
  11. done()
  12. })
  13. .ready(err => {
  14. console.log('Everything has been loaded')
  15. })

In case after() is called without a function, it returns a Promise:

  1. fastify.register(async (instance, opts) => {
  2. console.log('Current plugin')
  3. })
  4. await fastify.after()
  5. console.log('After current plugin')
  6. fastify.register(async (instance, opts) => {
  7. console.log('Next plugin')
  8. })
  9. await fastify.ready()
  10. console.log('Everything has been loaded')

ready

Function called when all the plugins have been loaded. It takes an error parameter if something went wrong.

  1. fastify.ready(err => {
  2. if (err) throw err
  3. })

If it is called without any arguments, it will return a Promise:

  1. fastify.ready().then(() => {
  2. console.log('successfully booted!')
  3. }, (err) => {
  4. console.log('an error happened', err)
  5. })

listen

Starts the server and internally waits for the .ready() event. The signature is .listen([options][, callback]). Both the options object and the callback parameters extend the Node.js core options object. Thus, all core options are available with the following additional Fastify specific options:

listenTextResolver

Set an optional resolver for the text to log after server has been successfully started. It is possible to override the default Server listening at [address] log entry using this option.

  1. server.listen({
  2. port: 9080,
  3. listenTextResolver: (address) => { return `Prometheus metrics server is listening at ${address}` }
  4. })

By default, the server will listen on the address(es) resolved by localhost when no specific host is provided. If listening on any available interface is desired, then specifying 0.0.0.0 for the address will listen on all IPv4 addresses. The following table details the possible values for host when targeting localhost, and what the result of those values for host will be.

HostIPv4IPv6
::*
:: + ipv6Only🚫
0.0.0.0🚫
localhost
127.0.0.1🚫
::1🚫

* Using :: for the address will listen on all IPv6 addresses and, depending on OS, may also listen on all IPv4 addresses.

Be careful when deciding to listen on all interfaces; it comes with inherent security risks.

The default is to listen on port: 0 (which picks the first available open port) and host: 'localhost':

  1. fastify.listen((err, address) => {
  2. if (err) {
  3. fastify.log.error(err)
  4. process.exit(1)
  5. }
  6. })

Specifying an address is also supported:

  1. fastify.listen({ port: 3000, host: '127.0.0.1' }, (err, address) => {
  2. if (err) {
  3. fastify.log.error(err)
  4. process.exit(1)
  5. }
  6. })

If no callback is provided a Promise is returned:

  1. fastify.listen({ port: 3000 })
  2. .then((address) => console.log(`server listening on ${address}`))
  3. .catch(err => {
  4. console.log('Error starting server:', err)
  5. process.exit(1)
  6. })

When deploying to a Docker, and potentially other, containers, it is advisable to listen on 0.0.0.0 because they do not default to exposing mapped ports to localhost:

  1. fastify.listen({ port: 3000, host: '0.0.0.0' }, (err, address) => {
  2. if (err) {
  3. fastify.log.error(err)
  4. process.exit(1)
  5. }
  6. })

If the port is omitted (or is set to zero), a random available port is automatically chosen (available via fastify.server.address().port).

The default options of listen are:

  1. fastify.listen({
  2. port: 0,
  3. host: 'localhost',
  4. exclusive: false,
  5. readableAll: false,
  6. writableAll: false,
  7. ipv6Only: false
  8. }, (err) => {})

addresses

This method returns an array of addresses that the server is listening on. If you call it before listen() is called or after the close() function, it will return an empty array.

  1. await fastify.listen({ port: 8080 })
  2. const addresses = fastify.addresses()
  3. // [
  4. // { port: 8080, family: 'IPv6', address: '::1' },
  5. // { port: 8080, family: 'IPv4', address: '127.0.0.1' }
  6. // ]

Note that the array contains the fastify.server.address() too.

routing

Method to access the lookup method of the internal router and match the request to the appropriate handler:

  1. fastify.routing(req, res)

route

Method to add routes to the server, it also has shorthand functions, check here.

hasRoute

Method to check if a route is already registered to the internal router. It expects an object as the payload. url and method are mandatory fields. It is possible to also specify constraints. The method returns true if the route is registered or false if not.

  1. const routeExists = fastify.hasRoute({
  2. url: '/',
  3. method: 'GET',
  4. constraints: { version: '1.0.0' } // optional
  5. })
  6. if (routeExists === false) {
  7. // add route
  8. }

findRoute

Method to retrieve a route already registered to the internal router. It expects an object as the payload. url and method are mandatory fields. It is possible to also specify constraints. The method returns a route object or null if the route cannot be found.

  1. const route = fastify.findRoute({
  2. url: '/artists/:artistId',
  3. method: 'GET',
  4. constraints: { version: '1.0.0' } // optional
  5. })
  6. if (route !== null) {
  7. // perform some route checks
  8. console.log(route.params) // `{artistId: ':artistId'}`
  9. }

close

fastify.close(callback): call this function to close the server instance and run the ‘onClose’ hook.

Calling close will also cause the server to respond to every new incoming request with a 503 error and destroy that request. See return503OnClosing flags for changing this behavior.

If it is called without any arguments, it will return a Promise:

  1. fastify.close().then(() => {
  2. console.log('successfully closed!')
  3. }, (err) => {
  4. console.log('an error happened', err)
  5. })

decorate*

Function useful if you need to decorate the fastify instance, Reply or Request, check here.

register

Fastify allows the user to extend its functionality with plugins. A plugin can be a set of routes, a server decorator, or whatever, check here.

addHook

Function to add a specific hook in the lifecycle of Fastify, check here.

prefix

The full path that will be prefixed to a route.

Example:

  1. fastify.register(function (instance, opts, done) {
  2. instance.get('/foo', function (request, reply) {
  3. // Will log "prefix: /v1"
  4. request.log.info('prefix: %s', instance.prefix)
  5. reply.send({ prefix: instance.prefix })
  6. })
  7. instance.register(function (instance, opts, done) {
  8. instance.get('/bar', function (request, reply) {
  9. // Will log "prefix: /v1/v2"
  10. request.log.info('prefix: %s', instance.prefix)
  11. reply.send({ prefix: instance.prefix })
  12. })
  13. done()
  14. }, { prefix: '/v2' })
  15. done()
  16. }, { prefix: '/v1' })

pluginName

Name of the current plugin. The root plugin is called 'fastify'. There are different ways to define a name (in order).

  1. If you use fastify-plugin the metadata name is used.
  2. If the exported plugin has the Symbol.for('fastify.display-name') property, then the value of that property is used. Example: pluginFn[Symbol.for('fastify.display-name')] = "Custom Name"
  3. If you module.exports a plugin the filename is used.
  4. If you use a regular function declaration the function name is used.

Fallback: The first two lines of your plugin will represent the plugin name. Newlines are replaced by --. This will help to identify the root cause when you deal with many plugins.

Warning If you have to deal with nested plugins, the name differs with the usage of the fastify-plugin because no new scope is created and therefore we have no place to attach contextual data. In that case, the plugin name will represent the boot order of all involved plugins in the format of fastify -> plugin-A -> plugin-B.

hasPlugin

Method to check if a specific plugin has been registered. Relies on the plugin metadata name. Returns true if the plugin is registered. Otherwise, returns false.

  1. const fastify = require('fastify')()
  2. fastify.register(require('@fastify/cookie'), {
  3. secret: 'my-secret',
  4. parseOptions: {}
  5. })
  6. fastify.ready(() => {
  7. fastify.hasPlugin('@fastify/cookie') // true
  8. })

listeningOrigin

The current origin the server is listening to. For example, a TCP socket based server returns a base address like http://127.0.0.1:3000, and a Unix socket server will return the socket path, e.g. fastify.temp.sock.

log

The logger instance, check here.

version

Fastify version of the instance. Used for plugin support. See Plugins for information on how the version is used by plugins.

inject

Fake HTTP injection (for testing purposes) here.

addHttpMethod

Fastify supports the GET, HEAD, TRACE, DELETE, OPTIONS, PATCH, PUT and POST HTTP methods by default. The addHttpMethod method allows to add any non standard HTTP methods to the server that are supported by Node.js.

  1. // Add a new HTTP method called 'MKCOL' that supports a request body
  2. fastify.addHttpMethod('MKCOL', { hasBody: true, })
  3. // Add a new HTTP method called 'COPY' that does not support a request body
  4. fastify.addHttpMethod('COPY')

After calling addHttpMethod, it is possible to use the route shorthand methods to define routes for the new HTTP method:

  1. fastify.addHttpMethod('MKCOL', { hasBody: true })
  2. fastify.mkcol('/', (req, reply) => {
  3. // Handle the 'MKCOL' request
  4. })

addSchema

fastify.addSchema(schemaObj), adds a JSON schema to the Fastify instance. This allows you to reuse it everywhere in your application just by using the standard $ref keyword.

To learn more, read the Validation and Serialization documentation.

getSchemas

fastify.getSchemas(), returns a hash of all schemas added via .addSchema. The keys of the hash are the $ids of the JSON Schema provided.

getSchema

fastify.getSchema(id), return the JSON schema added with .addSchema and the matching id. It returns undefined if it is not found.

setReplySerializer

Set the reply serializer for all the routes. This will be used as default if a Reply.serializer(func) has not been set. The handler is fully encapsulated, so different plugins can set different error handlers. Note: the function parameter is called only for status 2xx. Check out the setErrorHandler for errors.

  1. fastify.setReplySerializer(function (payload, statusCode){
  2. // serialize the payload with a sync function
  3. return `my serialized ${statusCode} content: ${payload}`
  4. })

setValidatorCompiler

Set the schema validator compiler for all routes. See #schema-validator.

setSchemaErrorFormatter

Set the schema error formatter for all routes. See #error-handling.

setSerializerCompiler

Set the schema serializer compiler for all routes. See #schema-serializer.

Note setReplySerializer has priority if set!

validatorCompiler

This property can be used to get the schema validator. If not set, it will be null until the server starts, then it will be a function with the signature function ({ schema, method, url, httpPart }) that returns the input schema compiled to a function for validating data. The input schema can access all the shared schemas added with .addSchema function.

serializerCompiler

This property can be used to get the schema serializer. If not set, it will be null until the server starts, then it will be a function with the signature function ({ schema, method, url, httpPart }) that returns the input schema compiled to a function for validating data. The input schema can access all the shared schemas added with .addSchema function.

schemaErrorFormatter

This property can be used to set a function to format errors that happen while the validationCompiler fails to validate the schema. See #error-handling.

schemaController

This property can be used to fully manage:

  • bucket: where the schemas of your application will be stored
  • compilersFactory: what module must compile the JSON schemas

It can be useful when your schemas are stored in another data structure that is unknown to Fastify.

Another use case is to tweak all the schemas processing. Doing so it is possible to use Ajv v8 JTD or Standalone feature. To use such as JTD or the Standalone mode, refers to the @fastify/ajv-compiler documentation.

  1. const fastify = Fastify({
  2. schemaController: {
  3. /**
  4. * This factory is called whenever `fastify.register()` is called.
  5. * It may receive as input the schemas of the parent context if some schemas have been added.
  6. * @param {object} parentSchemas these schemas will be returned by the
  7. * `getSchemas()` method function of the returned `bucket`.
  8. */
  9. bucket: function factory (parentSchemas) {
  10. return {
  11. add (inputSchema) {
  12. // This function must store the schema added by the user.
  13. // This function is invoked when `fastify.addSchema()` is called.
  14. },
  15. getSchema (schema$id) {
  16. // This function must return the raw schema requested by the `schema$id`.
  17. // This function is invoked when `fastify.getSchema(id)` is called.
  18. return aSchema
  19. },
  20. getSchemas () {
  21. // This function must return all the schemas referenced by the routes schemas' $ref
  22. // It must return a JSON where the property is the schema `$id` and the value is the raw JSON Schema.
  23. const allTheSchemaStored = {
  24. 'schema$id1': schema1,
  25. 'schema$id2': schema2
  26. }
  27. return allTheSchemaStored
  28. }
  29. }
  30. },
  31. /**
  32. * The compilers factory lets you fully control the validator and serializer
  33. * in the Fastify's lifecycle, providing the encapsulation to your compilers.
  34. */
  35. compilersFactory: {
  36. /**
  37. * This factory is called whenever a new validator instance is needed.
  38. * It may be called whenever `fastify.register()` is called only if new schemas have been added to the
  39. * encapsulation context.
  40. * It may receive as input the schemas of the parent context if some schemas have been added.
  41. * @param {object} externalSchemas these schemas will be returned by the
  42. * `bucket.getSchemas()`. Needed to resolve the external references $ref.
  43. * @param {object} ajvServerOption the server `ajv` options to build your compilers accordingly
  44. */
  45. buildValidator: function factory (externalSchemas, ajvServerOption) {
  46. // This factory function must return a schema validator compiler.
  47. // See [#schema-validator](./Validation-and-Serialization.md#schema-validator) for details.
  48. const yourAjvInstance = new Ajv(ajvServerOption.customOptions)
  49. return function validatorCompiler ({ schema, method, url, httpPart }) {
  50. return yourAjvInstance.compile(schema)
  51. }
  52. },
  53. /**
  54. * This factory is called whenever a new serializer instance is needed.
  55. * It may be called whenever `fastify.register()` is called only if new schemas have been added to the
  56. * encapsulation context.
  57. * It may receive as input the schemas of the parent context if some schemas have been added.
  58. * @param {object} externalSchemas these schemas will be returned by the
  59. * `bucket.getSchemas()`. Needed to resolve the external references $ref.
  60. * @param {object} serializerOptsServerOption the server `serializerOpts`
  61. * options to build your compilers accordingly
  62. */
  63. buildSerializer: function factory (externalSchemas, serializerOptsServerOption) {
  64. // This factory function must return a schema serializer compiler.
  65. // See [#schema-serializer](./Validation-and-Serialization.md#schema-serializer) for details.
  66. return function serializerCompiler ({ schema, method, url, httpStatus, contentType }) {
  67. return data => JSON.stringify(data)
  68. }
  69. }
  70. }
  71. }
  72. });

setNotFoundHandler

fastify.setNotFoundHandler(handler(request, reply)): set the 404 handler. This call is encapsulated by prefix, so different plugins can set different not found handlers if a different prefix option is passed to fastify.register(). The handler is treated as a regular route handler so requests will go through the full Fastify lifecycle. async-await is supported as well.

You can also register preValidation and preHandler hooks for the 404 handler.

Note The preValidation hook registered using this method will run for a route that Fastify does not recognize and not when a route handler manually calls reply.callNotFound. In which case, only preHandler will be run.

  1. fastify.setNotFoundHandler({
  2. preValidation: (req, reply, done) => {
  3. // your code
  4. done()
  5. },
  6. preHandler: (req, reply, done) => {
  7. // your code
  8. done()
  9. }
  10. }, function (request, reply) {
  11. // Default not found handler with preValidation and preHandler hooks
  12. })
  13. fastify.register(function (instance, options, done) {
  14. instance.setNotFoundHandler(function (request, reply) {
  15. // Handle not found request without preValidation and preHandler hooks
  16. // to URLs that begin with '/v1'
  17. })
  18. done()
  19. }, { prefix: '/v1' })

Fastify calls setNotFoundHandler to add a default 404 handler at startup before plugins are registered. If you would like to augment the behavior of the default 404 handler, for example with plugins, you can call setNotFoundHandler with no arguments fastify.setNotFoundHandler() within the context of these registered plugins.

Note Some config properties from the request object will be undefined inside the custom not found handler. E.g.: request.routerPath, routerMethod and context.config. This method design goal is to allow calling the common not found route. To return a per-route customized 404 response, you can do it in the response itself.

setErrorHandler

fastify.setErrorHandler(handler(error, request, reply)): Set a function that will be called whenever an error happens. The handler is bound to the Fastify instance and is fully encapsulated, so different plugins can set different error handlers. async-await is supported as well.

If the error statusCode is less than 400, Fastify will automatically set it to 500 before calling the error handler.

setErrorHandler will not catch:

  • errors thrown in an onResponse hook because the response has already been sent to the client. Use the onSend hook instead.
  • not found (404) errors. Use setNotFoundHandler instead.
  1. fastify.setErrorHandler(function (error, request, reply) {
  2. // Log error
  3. this.log.error(error)
  4. // Send error response
  5. reply.status(409).send({ ok: false })
  6. })

Fastify is provided with a default function that is called if no error handler is set. It can be accessed using fastify.errorHandler and it logs the error with respect to its statusCode.

  1. const statusCode = error.statusCode
  2. if (statusCode >= 500) {
  3. log.error(error)
  4. } else if (statusCode >= 400) {
  5. log.info(error)
  6. } else {
  7. log.error(error)
  8. }

setChildLoggerFactory

fastify.setChildLoggerFactory(factory(logger, bindings, opts, rawReq)): Set a function that will be called when creating a child logger instance for each request which allows for modifying or adding child logger bindings and logger options, or returning a custom child logger implementation.

Child logger bindings have a performance advantage over per-log bindings because they are pre-serialized by Pino when the child logger is created.

The first parameter is the parent logger instance, followed by the default bindings and logger options which should be passed to the child logger, and finally the raw request (not a Fastify request object). The function is bound with this being the Fastify instance.

For example:

  1. const fastify = require('fastify')({
  2. childLoggerFactory: function (logger, bindings, opts, rawReq) {
  3. // Calculate additional bindings from the request if needed
  4. bindings.traceContext = rawReq.headers['x-cloud-trace-context']
  5. return logger.child(bindings, opts)
  6. }
  7. })

The handler is bound to the Fastify instance and is fully encapsulated, so different plugins can set different logger factories.

setGenReqId

fastify.setGenReqId(function (rawReq)) Synchronous function for setting the request-id for additional Fastify instances. It will receive the raw incoming request as a parameter. The provided function should not throw an Error in any case.

Especially in distributed systems, you may want to override the default ID generation behavior to handle custom ways of generating different IDs in order to handle different use cases. Such as observability or webhooks plugins.

For example:

  1. const fastify = require('fastify')({
  2. genReqId: (req) => {
  3. return 'base'
  4. }
  5. })
  6. fastify.register((instance, opts, done) => {
  7. instance.setGenReqId((req) => {
  8. // custom request ID for `/webhooks`
  9. return 'webhooks-id'
  10. })
  11. done()
  12. }, { prefix: '/webhooks' })
  13. fastify.register((instance, opts, done) => {
  14. instance.setGenReqId((req) => {
  15. // custom request ID for `/observability`
  16. return 'observability-id'
  17. })
  18. done()
  19. }, { prefix: '/observability' })

The handler is bound to the Fastify instance and is fully encapsulated, so different plugins can set a different request ID.

addConstraintStrategy

Function to add a custom constraint strategy. To register a new type of constraint, you must add a new constraint strategy that knows how to match values to handlers, and that knows how to get the constraint value from a request.

Add a custom constraint strategy using the fastify.addConstraintStrategy method:

  1. const customResponseTypeStrategy = {
  2. // strategy name for referencing in the route handler `constraints` options
  3. name: 'accept',
  4. // storage factory for storing routes in the find-my-way route tree
  5. storage: function () {
  6. let handlers = {}
  7. return {
  8. get: (type) => { return handlers[type] || null },
  9. set: (type, store) => { handlers[type] = store }
  10. }
  11. },
  12. // function to get the value of the constraint from each incoming request
  13. deriveConstraint: (req, ctx) => {
  14. return req.headers['accept']
  15. },
  16. // optional flag marking if handlers without constraints can match requests that have a value for this constraint
  17. mustMatchWhenDerived: true
  18. }
  19. const router = Fastify();
  20. router.addConstraintStrategy(customResponseTypeStrategy);

hasConstraintStrategy

The fastify.hasConstraintStrategy(strategyName) checks if there already exists a custom constraint strategy with the same name.

printRoutes

fastify.printRoutes(): Fastify router builds a tree of routes for each HTTP method. If you call the prettyPrint without specifying an HTTP method, it will merge all the trees into one and print it. The merged tree doesn’t represent the internal router structure. Do not use it for debugging.

Remember to call it inside or after a ready call.

  1. fastify.get('/test', () => {})
  2. fastify.get('/test/hello', () => {})
  3. fastify.get('/testing', () => {})
  4. fastify.get('/testing/:param', () => {})
  5. fastify.put('/update', () => {})
  6. fastify.ready(() => {
  7. console.log(fastify.printRoutes())
  8. // └── /
  9. // ├── test (GET)
  10. // │ ├── /hello (GET)
  11. // │ └── ing (GET)
  12. // │ └── /
  13. // │ └── :param (GET)
  14. // └── update (PUT)
  15. })

If you want to print the internal router tree, you should specify the method param. Printed tree will represent the internal router structure. You can use it for debugging.

  1. console.log(fastify.printRoutes({ method: 'GET' }))
  2. // └── /
  3. // └── test (GET)
  4. // ├── /hello (GET)
  5. // └── ing (GET)
  6. // └── /
  7. // └── :param (GET)
  8. console.log(fastify.printRoutes({ method: 'PUT' }))
  9. // └── /
  10. // └── update (PUT)

fastify.printRoutes({ commonPrefix: false }) will print compressed trees. This may be useful when you have a large number of routes with common prefixes. It doesn’t represent the internal router structure. Do not use it for debugging.

  1. console.log(fastify.printRoutes({ commonPrefix: false }))
  2. // ├── /test (GET)
  3. // │ ├── /hello (GET)
  4. // │ └── ing (GET)
  5. // │ └── /:param (GET)
  6. // └── /update (PUT)

fastify.printRoutes({ includeMeta: (true | []) }) will display properties from the route.store object for each displayed route. This can be an array of keys (e.g. ['onRequest', Symbol('key')]), or true to display all properties. A shorthand option, fastify.printRoutes({ includeHooks: true }) will include all hooks.

  1. fastify.get('/test', () => {})
  2. fastify.get('/test/hello', () => {})
  3. const onTimeout = () => {}
  4. fastify.addHook('onRequest', () => {})
  5. fastify.addHook('onTimeout', onTimeout)
  6. console.log(fastify.printRoutes({ includeHooks: true, includeMeta: ['errorHandler'] }))
  7. // └── /
  8. // └── test (GET)
  9. // • (onTimeout) ["onTimeout()"]
  10. // • (onRequest) ["anonymous()"]
  11. // • (errorHandler) "defaultErrorHandler()"
  12. // test (HEAD)
  13. // • (onTimeout) ["onTimeout()"]
  14. // • (onRequest) ["anonymous()"]
  15. // • (onSend) ["headRouteOnSendHandler()"]
  16. // • (errorHandler) "defaultErrorHandler()"
  17. // └── /hello (GET)
  18. // • (onTimeout) ["onTimeout()"]
  19. // • (onRequest) ["anonymous()"]
  20. // • (errorHandler) "defaultErrorHandler()"
  21. // /hello (HEAD)
  22. // • (onTimeout) ["onTimeout()"]
  23. // • (onRequest) ["anonymous()"]
  24. // • (onSend) ["headRouteOnSendHandler()"]
  25. // • (errorHandler) "defaultErrorHandler()"
  26. console.log(fastify.printRoutes({ includeHooks: true }))
  27. // └── /
  28. // └── test (GET)
  29. // • (onTimeout) ["onTimeout()"]
  30. // • (onRequest) ["anonymous()"]
  31. // test (HEAD)
  32. // • (onTimeout) ["onTimeout()"]
  33. // • (onRequest) ["anonymous()"]
  34. // • (onSend) ["headRouteOnSendHandler()"]
  35. // └── /hello (GET)
  36. // • (onTimeout) ["onTimeout()"]
  37. // • (onRequest) ["anonymous()"]
  38. // /hello (HEAD)
  39. // • (onTimeout) ["onTimeout()"]
  40. // • (onRequest) ["anonymous()"]
  41. // • (onSend) ["headRouteOnSendHandler()"]

printPlugins

fastify.printPlugins(): Prints the representation of the internal plugin tree used by the avvio, useful for debugging require order issues.

Remember to call it inside or after a ready call.

  1. fastify.register(async function foo (instance) {
  2. instance.register(async function bar () {})
  3. })
  4. fastify.register(async function baz () {})
  5. fastify.ready(() => {
  6. console.error(fastify.printPlugins())
  7. // will output the following to stderr:
  8. // └── root
  9. // ├── foo
  10. // │ └── bar
  11. // └── baz
  12. })

addContentTypeParser

fastify.addContentTypeParser(content-type, options, parser) is used to pass a custom parser for a given content type. Useful for adding parsers for custom content types, e.g. text/json, application/vnd.oasis.opendocument.text. content-type can be a string, string array or RegExp.

  1. // The two arguments passed to getDefaultJsonParser are for ProtoType poisoning
  2. // and Constructor Poisoning configuration respectively. The possible values are
  3. // 'ignore', 'remove', 'error'. ignore skips all validations and it is similar
  4. // to calling JSON.parse() directly. See the
  5. // [`secure-json-parse` documentation](https://github.com/fastify/secure-json-parse#api) for more information.
  6. fastify.addContentTypeParser('text/json', { asString: true }, fastify.getDefaultJsonParser('ignore', 'ignore'))

hasContentTypeParser

fastify.hasContentTypeParser(contentType) is used to check whether there is a content type parser in the current context for the specified content type.

  1. fastify.hasContentTypeParser('text/json')
  2. fastify.hasContentTypeParser(/^.+\/json$/)

removeContentTypeParser

fastify.removeContentTypeParser(contentType) is used to remove content type parsers in the current context. This method allows for example to remove the both built-in parsers for application/json and text/plain.

  1. fastify.removeContentTypeParser('application/json')
  2. fastify.removeContentTypeParser(['application/json', 'text/plain'])

removeAllContentTypeParsers

The fastify.removeAllContentTypeParsers() method allows all content type parsers in the current context to be removed. A use case of this method is the implementation of catch-all content type parser. Before adding this parser with fastify.addContentTypeParser() one could call the removeAllContentTypeParsers method.

For more details about the usage of the different content type parser APIs see here.

getDefaultJsonParser

fastify.getDefaultJsonParser(onProtoPoisoning, onConstructorPoisoning) takes two arguments. First argument is ProtoType poisoning configuration and second argument is constructor poisoning configuration. See the secure-json-parse documentation for more information.

defaultTextParser

fastify.defaultTextParser() can be used to parse content as plain text.

  1. fastify.addContentTypeParser('text/json', { asString: true }, fastify.defaultTextParser)

errorHandler

fastify.errorHandler can be used to handle errors using fastify’s default error handler.

  1. fastify.get('/', {
  2. errorHandler: (error, request, reply) => {
  3. if (error.code === 'SOMETHING_SPECIFIC') {
  4. reply.send({ custom: 'response' })
  5. return
  6. }
  7. fastify.errorHandler(error, request, response)
  8. }
  9. }, handler)

childLoggerFactory

fastify.childLoggerFactory returns the custom logger factory function for the Fastify instance. See the childLoggerFactory config option for more info.

Symbol.asyncDispose

fastify[Symbol.asyncDispose] is a symbol that can be used to define an asynchronous function that will be called when the Fastify instance is closed.

It’s commonly used alongside the using TypeScript keyword to ensure that resources are cleaned up when the Fastify instance is closed.

This combines perfectly inside short lived processes or unit tests, where you must close all Fastify resources after returning from inside the function.

  1. test('Uses app and closes it afterwards', async () => {
  2. await using app = fastify();
  3. // do something with app.
  4. })

In the above example, Fastify is closed automatically after the test finishes.

Read more about the ECMAScript Explicit Resource Management and the using keyword introduced in TypeScript 5.2.

initialConfig

fastify.initialConfig: Exposes a frozen read-only object registering the initial options passed down by the user to the Fastify instance.

The properties that can currently be exposed are:

  • connectionTimeout
  • keepAliveTimeout
  • bodyLimit
  • caseSensitive
  • allowUnsafeRegex
  • http2
  • https (it will return false/true or { allowHTTP1: true/false } if explicitly passed)
  • ignoreTrailingSlash
  • disableRequestLogging
  • maxParamLength
  • onProtoPoisoning
  • onConstructorPoisoning
  • pluginTimeout
  • requestIdHeader
  • requestIdLogLabel
  • http2SessionTimeout
  • useSemicolonDelimiter
  1. const { readFileSync } = require('node:fs')
  2. const Fastify = require('fastify')
  3. const fastify = Fastify({
  4. https: {
  5. allowHTTP1: true,
  6. key: readFileSync('./fastify.key'),
  7. cert: readFileSync('./fastify.cert')
  8. },
  9. logger: { level: 'trace'},
  10. ignoreTrailingSlash: true,
  11. maxParamLength: 200,
  12. caseSensitive: true,
  13. trustProxy: '127.0.0.1,192.168.1.1/24',
  14. })
  15. console.log(fastify.initialConfig)
  16. /*
  17. will log :
  18. {
  19. caseSensitive: true,
  20. https: { allowHTTP1: true },
  21. ignoreTrailingSlash: true,
  22. maxParamLength: 200
  23. }
  24. */
  25. fastify.register(async (instance, opts) => {
  26. instance.get('/', async (request, reply) => {
  27. return instance.initialConfig
  28. /*
  29. will return :
  30. {
  31. caseSensitive: true,
  32. https: { allowHTTP1: true },
  33. ignoreTrailingSlash: true,
  34. maxParamLength: 200
  35. }
  36. */
  37. })
  38. instance.get('/error', async (request, reply) => {
  39. // will throw an error because initialConfig is read-only
  40. // and can not be modified
  41. instance.initialConfig.https.allowHTTP1 = false
  42. return instance.initialConfig
  43. })
  44. })
  45. // Start listening.
  46. fastify.listen({ port: 3000 }, (err) => {
  47. if (err) {
  48. fastify.log.error(err)
  49. process.exit(1)
  50. }
  51. })