koa-static-cache插件:
处理服务器静态文件,自带缓存机制。
用法:server.use(staticCache('路径'))
koa-static-cache基本用法:
const Koa = require('koa');
const path = require('path');
const staticCache = require('koa-static-cache');
const server = new Koa();
// 访问地址:http://localhost:3000/index.html,相当于访问www文件下的index.html
server.use(staticCache(path.resolve('www')));
server.listen(3000, () => {
console.log('Server is running');
});
补充:path模块
-
path.resolve()
,将字符串计算成绝对路径,但不会考虑该路径下是否存在该文件
const path = require('path');
console.log(path.resolve('www')); // C:\Users\Project\Node\www
console.log(path.resolve('www', 'a.html')); // C:\Users\Project\Node\www\a.html
console.log(path.resolve('www', 'a.html', '../b')); // C:\Users\Project\Node\www\b
网友评论