Skip to content

A simple, lightweight, plugin for Fastify that allows you to easily host static files.

License

Notifications You must be signed in to change notification settings

Matt-DESTROYER/fastify-streamfile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version Install size Minzipped size Downloads/month

fastify-streamfile

A lightweight package that adds essential, easy-to-use file sending functionality using decorators.

Using fastify-streamfile

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")));

Methods

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).

About

A simple, lightweight, plugin for Fastify that allows you to easily host static files.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published