https://github.com/ladjs/koa-better-static
Static file serving middleware for Koa. Forked from an inactive project and maintained for Lad.
https://github.com/ladjs/koa-better-static
Last synced: 9 months ago
JSON representation
Static file serving middleware for Koa. Forked from an inactive project and maintained for Lad.
- Host: GitHub
- URL: https://github.com/ladjs/koa-better-static
- Owner: ladjs
- License: mit
- Created: 2019-01-06T22:40:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-06T16:10:11.000Z (about 2 years ago)
- Last Synced: 2024-09-23T12:45:14.917Z (over 1 year ago)
- Language: JavaScript
- Homepage: https://lad.js.org
- Size: 246 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [**@ladjs/koa-better-static**](https://github.com/ladjs/koa-better-static)
[](https://github.com/ladjs/koa-better-static/actions/workflows/ci.yml)
[](https://github.com/sindresorhus/xo)
[](https://github.com/prettier/prettier)
[](https://lass.js.org)
[](LICENSE)
[](https://npm.im/@ladjs/koa-better-static)
> Static file serving middleware for Koa. Forked from an inactive project and maintained for Lad.
## Table of Contents
* [Features](#features)
* [Install](#install)
* [Usage](#usage)
* [Options](#options)
* [Contributors](#contributors)
* [License](#license)
## Features
Our package `@ladjs/koa-better-static` is a high-performance, drop-in replacement for `koa-static` and is a modernized, Koa 2.x+ fork of `koa-better-static` (which is unmaintained and inactive).
* Uses a more optimal version of `koa-send`
* Supports `If-Modified-Since` header for cache/performance
* Removal of `gzip` option (which checks for .gz files)
* Removal of `defer` (if you want this behavior, put the middleware at the end)
* No default `index` file
* Faster performance with `findIndex` hidden file lookup
* Properly sets `Content-Type` header using `mime-types`
## Install
[npm][]:
```sh
npm install @ladjs/koa-better-static
```
## Usage
This package exposes a function which accepts two arguments `root` (String - the path to serve assets from) and `opts` (an Object of options, see [Options](#options) below).
```js
const serve = require('@ladjs/koa-better-static');
const koa = require('koa');
const app = koa();
// GET /package.json
app.use(serve('.'));
// GET /hello.txt
app.use(serve('test/fixtures'));
// or use absolute paths
app.use(serve(__dirname + '/test/fixtures'));
app.listen(3000, () => {
console.log('listening on port 3000');
});
```
## Options
* `maxage` Browser cache max-age in milliseconds. defaults to `0` (`maxAge` camelCase also works as a valid option alternative)
* `hidden` Allow transfer of hidden files. defaults to false
* `index` Default file name, defaults to none
* `ifModifiedSinceSupport` by sending a 304 (not modified) response. Defaults to true
* `format` Allow trailing slashes for directories (e.g. /directory and /directory. Defaults to true
## Contributors
| Name | Website |
| -------------- | -------------------------- |
| **Nick Baugh** | |
## License
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)
##
[npm]: https://www.npmjs.com/