Plugin for serving static files as fast as possible.
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 '/'
page404Path: path.join(__dirname, 'public', '404.html'), // optional
page403Path: path.join(__dirname, 'public', '403.html'), // optional
page500Path: path.join(__dirname, 'public', '500.html') // optional
})
fastify.get('/another/path', function (req, reply) {
reply.sendFile('myHtml.html') // serving path.join(__dirname, 'public', 'myHtml.html') directly
})
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.
The absolute path to an HTML file to send as a response for the corresponding error status code. A generic error page is sent by default.
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.
The following options are also supported and will be passed directly to the
send
module:
Licensed under MIT