koa2-static-middleware
v1.0.3
Published
Static file serving middleware for Koa 2, compatible with Koa-Router
Downloads
244
Readme
koa2-static-middleware
A very simple wrapper for koa-send. Sends files from specified directory. Adapted from MatheusMK3/koa-router-static. Compatible with alexmingoia/koa-router. Serves static files at directory.
Install
npm i koa2-static-middleware
API
serve(path, opts)
Returns middleware function with applied settings.
path
: String local directory from where to serve assets
opts
: Object {
opts.index
: string file to serve from root path. default: index.html
opts.debug
: boolean prints info to console on initial middleware producing function call. default: false
}
Example usage (with alexmingoia/koa-router)
const Koa = require('koa');
const Router = require('koa-router');
const serve = require('koa2-static-middleware');
const app = new Koa();
const router = new Router();
router.get('/assets/*', serve('./dist', { index: 'index.htm' }));
app.use(router.routes());
app.listen(8000, () => console.log('Serving local files from ./dist directory at http://localhost:8000/assets/'));
Example usage (without alexmingoia/koa-router)
const Koa = require('koa');
const serve = require('koa2-static-middleware');
const app = new Koa();
app.use(serve('./dist');
app.listen(8000, () => console.log('Serving local files from ./dist directory at http://localhost:8000/'));
Quirks
This module determines whether alexmingoia/koa-router
is being used through duck-typing, specifically it checks whether ctx.params
is set or not. If it is, it will use koa-router routes. So if you are using other middleware that hijacks the params
property on ctx
, you may run into problems.