配置静态资源的中间件

const Koa = require('koa');
const app = new Koa();
app.use(require('koa-static')(root, opts));

root:是配置的路径

opts:是可选参数 

    maxage:可缓存的时间

使用方法

const serve = require('koa-static');
const Koa = require('koa');
const app = new 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');

 

posted on 2017-03-14 11:10  坚持不懈❥  阅读(856)  评论(0编辑  收藏  举报