A static files serving middleware for koa.
$ npm install koa-files
const Koa = require('koa');
const server = require('koa-files');
const app = new Koa();
// Static files server
app.use(server(root, options));
root
root directory string. nothing above this root directory can be served.options
options object.
- Enable or disable accepting ranged requests. Disabling this will not send Accept-Ranges and ignore the contents of the Range request header. defaults to
true
.
- Set Cache-Control response header, defaults to
undefined
, see docs: Cache-Control in MDN.
- Enable or disable etag generation, defaults to
true
.
- Enable or disable Last-Modified header, defaults to true. Uses the file system's last modified value. defaults to
true
.
- Set ignore rules. defaults to
undefined
.
- If true, serves after
await next()
, allowing any downstream middleware to respond first. defaults tofalse
.
/**
* @module server
* @license MIT
* @author nuintun
*/
'use strict';
const Koa = require('koa');
const server = require('koa-files');
const app = new Koa();
const port = process.env.PORT || 80;
/**
* @function httpError
* @param {NodeJS.ErrnoException} error
* @returns {boolean}
*/
function httpError(error) {
return /^(EOF|EPIPE|ECANCELED|ECONNRESET|ECONNABORTED)$/i.test(error.code);
}
// Static files server
app.use(server('tests', { cacheControl: 'public, max-age=31557600' }));
// Listen error event
app.on('error', error => !httpError(error) && console.error(error));
// Start server
app.listen(port, () => console.log(`> server running at: 127.0.0.1:${port}`));
Support multipart range and download resumption.
MIT