Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

koa-better-static

ohomer45MIT1.0.5

Static file serving middleware for koa

koa, middleware, file, static, sendfile

readme

koa-better-static

A higher performance, drop in replacement for koa-static, with a few minimal changes:

  • Doesn't use koa-send, but uses an optimized version
  • 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
  • Requires node 4 or greater.

Installation

$ npm install koa-better-static

API

var koa = require('koa');
var app = koa();
app.use(require('koa-static')(root, opts));
  • opts options object.

Options

  • maxage Browser cache max-age in milliseconds. defaults to 0
  • 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

Example

var serve = require('koa-better-static');
var koa = require('koa');
var 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');

License

MIT

changelog

2.0.0 / 2016-01-07

  • bump koa-send@~3.1.0

1.5.2 / 2015-11-03

  • Fix: default index could be disabled. Closes #41

1.5.1 / 2015-10-14

  • Fix v1.4.x → 1.5.0 broken. Closes #53

1.5.0 / 2015-10-14

  • update koa-send@2
  • update devDeps

1.4.9 / 2015-02-03

  • only support GET and HEAD requests

1.4.8 / 2014-12-17

  • support root = .

1.4.7 / 2014-09-07

  • update koa-send

1.4.5 / 2014-05-05

  • Fix response already handled logic - Koajs now defaults status == 404. See koajs/koa#269

1.4.4 / 2014-05-04

  • Add two missing semicolons. Closes #24
  • Use bash syntax highlighting for install command. Closes #23
  • named generator function to help debugging. Closes #20

1.4.3 / 2014-02-11

  • update koa-send

1.4.2 / 2014-01-07

  • update koa-send

1.4.1 / 2013-12-30

  • fix for koa 0.2.1. Closes #12

1.4.0 / 2013-12-20

  • add: defer option - semi-breaking change

1.3.0 / 2013-12-11

  • refactor to use koa-send
  • rename maxAge -> maxage
  • fix: don't bother responding if response already "handled"

1.2.0 / 2013-09-14

  • add Last-Modified. Closes #5

1.1.1 / 2013-09-13

  • fix upstream responses

1.1.0 / 2013-09-13

  • rewrite without send