koa-static
cnpm i koa-static -d
const static = requiere('koa-static')
新建static文件夹
static文件夹下面新建1.html
server.js加上
server.use(static('./static'));
即可
let staticRouter = new Router();
server.use(static('./static'),{
maxage:864000*1000, //缓存时间,可降低服务器压力
index: '1.html' //默认文件
})
staticRouter.all(/(\.jpg|\.png|\.gif)/i,static('./static',{
maxage:60*86400*1000
})) //如果是jpg,png,gif文件那就缓存两个月
staticRouter.all(/(\.css)$/i,static('./static',{
maxage:1*86400*1000
})); //如果是css文件那就缓存一天
staticRouter.all(/(\.html|\.htm|\.shtml)$/i,static('./static',{
maxage:20*86400*1000
}));
staticRouter.all('',static('./static',{
maxage:30*86400*1000
})); //其他文件缓存30天
server.use(staticRouter.roures());
-----------------------------------------------------
koa-beffer-body
cnpm i koa-better-body -D
ctx.request.fields
const Koa = require('koa');
const Router = require('koa-router');
consst body = require('koa-better-body');
let server = new Koa();
server.listen(8520);
server.use(body({
//上传的文件储存在这里
uploadDir:'./static/upload'
}))
server.use(async ctx=>{
//文件和post数据
console.log(ctx.request.fields);
ctx.body='aaa';
});
接着form表单设置文件上传enctype='multipart/.....'
-------------------------------------------------------
koa里cookie是自带的
const Koa = require('koa');
const Router = require('koa-router');
let server = new Koa();
server.listen(8520);
server.keys=['asdfasdf','asdfasdfa',''...]; //滚动秘钥,加密
server.use(async ctx=>{
ctx.cookies.set('user','blue',{
maxAge: 14*86400*1000,
signed: true //签名
});
ctx.cookies.get();
})