This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

@donutteam/koa-static
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Koa Static

A class for creating Koa middlewares that serve files from one or more directories.

This middleware is built on top of koa-static-cache.

Installation

Install the package with NPM:

npm install @donutteam/koa-static

Usage

To use this class, simply instantiate an instance and add it to your Koa stack:

import Koa from "koa";

import { StaticMiddleware } from "@donutteam/koa-static";

const app = new Koa();

const staticMiddleware = new StaticMiddleware(
	{
		dirs:
		[
			// ...
		],
	});

// Be sure to add the execute function on the instance
// and NOT the instance itself
app.use(staticMiddleware.execute);

Options

dirs

Specify one or more directories to serve static files from. Later entries will override earlier ones.

You must specify at least one path.

import { StaticMiddleware } from "@donutteam/koa-static";

import { path } from "node:path";

const staticMiddleware = new StaticMiddleware(
	{
		dirs:
		[
			// Maybe you have a package that just contains shared static assets?
			path.join(process.cwd(), "node_modules", "@scope", "static", "public"),

			// Serve from the "public" folder in the app's working directory
			// Files in this folder will override files in any previous folders
			path.join(process.cwd(), "public"),	
		],
	});

koaStaticCache

An object containing options for the underlying koa-static-cache instance(s).

See this document for more details.

Methods

getCacheBustedPath

This returns the path to the given asset with a cache buster query parameter appended to it.

If the file does not exist in any of the static directories, the path will be returned as is.

const cacheBustedPath = staticMiddleware.getCacheBustedPath("/index.css");

// Returns: /index.css?mtime=1657678626915

License

MIT

Package Sidebar

Install

npm i @donutteam/koa-static

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

8.25 kB

Total Files

5

Last publish

Collaborators

  • duckdotexe