A lightweight package that adds essential, easy-to-use file sending functionality using decorators.
import FastifyStreamFile from "fastify-streamfile";
fastify.register(FastifyStreamFile);
// (optional) setup a static folder
// get `__dirname`
import { fileURLToPath } from "node:url";
import path from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// this should be (and will cause errors if not) run after the plugin is registered
fastify.after(() => fastify.static(path.join(__dirname, "static")));These are methods exported to the reply object.
reply.streamFile(String file);
reply.streamFile(String file, String root);Sends a file using the input filename (file) and located in the current working directory, or optionaly via the root, as the response, also setting the appropriate type using the file's extension.
reply.sendFile(String file);
reply.sendFile(String file, String root);
reply.sendFile(String file, function callback);
reply.sendFile(String file, String root, function callback);Sends a file using the input filename (file) and located in the current working directory, or optionally via the root, as the response, also setting the appropriate type using the file's extension. You can also manipulate the file's content using an optional callback.
These are methods exported to the fastify object.
fastify.static(String folder);
fastify.static(String folder, String root);
fastify.static(String folder, String root, String route);
fastify.static(String folder, String root, String route, Integer depth)Hosts all files in the input folder (folder) at the input route (route), recursing through folders to an input depth (depth, which defaults to 10).