Plugin for serving static file as fast as possible
Clone or download
Latest commit e43a9e7 Dec 5, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
example nits Apr 10, 2018
test Add attributes file Dec 3, 2018
.gitattributes Add attributes file Dec 3, 2018
.gitignore Fix for fastify break compatibility Nov 18, 2017
.travis.yml Dropped support for Node 9 Aug 13, 2018
LICENSE Updated copyright year to 2018 Feb 2, 2018 Added 1.x notice Dec 4, 2018
index.js Support only fastify v2 Dec 4, 2018
package.json 2.0.0 Dec 5, 2018


Build Status Greenkeeper badge Known Vulnerabilities

Plugin for serving static files as fast as possible. Supports Fastify versions >=2.0.0.

Please refer to this branch and related versions for Fastify ^1.11.0 compatibility.


npm install --save fastify-static


const fastify = require('fastify')()
const path = require('path')

fastify.register(require('fastify-static'), {
  root: path.join(__dirname, 'public'),
  prefix: '/public/', // optional: default '/'

fastify.get('/another/path', function (req, reply) {
  reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly


root (required)

The absolute path of the directory that contains the files to serve. The file to serve will be determined by combining req.url with the provided root directory.


Default: '/'

A URL path prefix used to create a virtual mount path for the static directory.


Default: true

A flag that define if the fastify route hide-schema attribute is hidden or not


Default: undefined

A function to set custom headers on the response. Alterations to the headers must be done synchronously. The function is called as fn(res, path, stat), where the arguments are:

  • res The response object.
  • path The path of the file that is being sent.
  • stat The stat object of the file that is being sent.

send Options

The following options are also supported and will be passed directly to the send module:

Disable serving

If you'd just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option { serve: false }. This will prevent the plugin from serving everything under root.

Disabling reply decorator

The reply object is decorated with a sendFile function by default. If you want to disable this, pass the option { decorateReply: false }. If fastify-static is registers to multiple prefixes in the same route only one can initialize reply decorators.

Handling 404s

If a request matches the URL prefix but a file cannot be found for the request, Fastify's 404 handler will be called. You can set a custom 404 handler with fastify.setNotFoundHandler().

Handling Errors

If an error occurs while trying to send a file, the error will be passed to Fastify's error handler. You can set a custom error handler with fastify.setErrorHandler().

Payload stream.filename

If you need to access the filename inside the onSend hook, you can use payload.filename.

fastify.addHook('onSend', function (req, reply, payload, next) {


Licensed under MIT