What is fastify-static?
The fastify-static package is a Fastify plugin that allows you to serve static files, such as HTML, CSS, JavaScript, images, and other assets, from a directory. It is designed to be highly performant and easy to use, leveraging Fastify's speed and low overhead.
What are fastify-static's main functionalities?
Serve Static Files
This feature allows you to serve static files from a specified directory. In this example, files from the 'public' directory are served with the URL prefix '/public/'.
const fastify = require('fastify')();
const path = require('path');
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/', // optional: default is '/'
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Serve Single Page Applications (SPA)
This feature is useful for serving Single Page Applications (SPA). When a route is not found, it serves the 'index.html' file, allowing the client-side router to handle the navigation.
const fastify = require('fastify')();
const path = require('path');
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
wildcard: false
});
fastify.setNotFoundHandler((request, reply) => {
reply.sendFile('index.html');
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Serve Files with Custom Headers
This feature allows you to set custom headers for the served files. In this example, a custom header 'X-Custom-Header' is added to all responses.
const fastify = require('fastify')();
const path = require('path');
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
setHeaders: (res, path, stat) => {
res.setHeader('X-Custom-Header', 'fastify-static');
}
});
fastify.listen(3000, err => {
if (err) throw err;
console.log('Server listening on http://localhost:3000');
});
Other packages similar to fastify-static
serve-static
The serve-static package is a middleware for serving static files in Express applications. It is similar to fastify-static but is designed for use with the Express framework. It offers similar functionality, such as serving files from a directory and setting custom headers.
koa-static
The koa-static package is a middleware for serving static files in Koa applications. Like fastify-static, it allows you to serve files from a directory and supports features like setting custom headers. It is designed to work with the Koa framework.
fastify-static
Plugin for serving static file
Install
npm install --save fastify-static
Usage
const fastify = require('fastify')
const path = require('path')
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/'
page404Path: path.join(__dirname, 'public', '404.html'),
page403Path: path.join(__dirname, 'public', '403.html'),
page500Path: path.join(__dirname, 'public', '500.html')
})
fastify.get('/another/path', function (req, reply) {
reply.sendFile('myHtml.html')
})
License
Licensed under MIT