Register @fastify/static to serve files from a specific directory. By default, files are served from the root directory at the / prefix. You can use reply.sendFile(path) to serve specific files within your route handlers.
const fastify = require('fastify')({logger: true})
const path = require('node: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
})
fastify.listen({ port: 3000 }, (err, address) => {
if (err) throw err
})