npm i koa複製代碼
npm i koa-router複製代碼
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
let router = new Router();
app.get('/',async ctx => {
ctx.body = 'Index Page';
})
app.use(router.routes());
app.listen(3000);複製代碼
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
let router = new Router();
let userRouter = new Router();
userRouter.get('/', async ctx=>{
ctx.body = 'user';
})
let company = new Router();
company.get('a',ctx=>{
ctx.body = 'company Page';
})
let admin = new Router();
admin.get('b',ctx=>{
ctx.body = 'admin Page';
})
userRouter.use(company.routes());
userRouter.use(admin.routes());
router.use(userRouter.routes());
app.use(router.routes());
app.listen(3000);複製代碼
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
let router = new Router();
app.get('/user/:id',async ctx => {
// 參數能夠從ctx中的params中獲取
console.log(ctx.params);
//獲取id的值
let { id } = ctx.params;
console.log(id);
})
app.use(router.routes());
app.listen(3000);複製代碼
urlencoded格式: http://www.baidu.com/user?id=xxxcss
獲取urlencoded路由的參數html
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
let router = new Router();
app.get('/user/',async ctx => {
// urlencoded傳參數能夠從ctx中的query中獲取
console.log(ctx.query);
//獲取id的值
let { id } = ctx.query;
console.log(id);
})
app.use(router.routes());
app.listen(3000);複製代碼
params格式: http:/www.baidu.com/user/12 12=>:idmysql
app.context是app服務中ctx的原型,若是須要添加全局組件,或者全局變量,能夠使用app.context來設置全局react
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
// 將數據庫設置爲全局
app.context.db = mysql.createPool();
let router = new Router();
app.get('/',async ctx => {
ctx.db.query('xxxxxxxx');
})
app.use(router.routes());
app.listen(3000);複製代碼
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
// 將數據庫設置爲全局
app.context.db = mysql.createPool();
let router = new Router();
app.get('/login',async ctx => {
let { username,password } = ctx.query;
if( !username || !password ){
ctx.throw(404,'username and password are required');
}
})
app.use(router.routes());
app.listen(3000);複製代碼
const Koa = require('koa');
const Router = require('koa-router');
let app = new Koa();
// 將數據庫設置爲全局
app.context.db = mysql.createPool();
let router = new Router();
app.get('/login',async ctx => {
ctx.assert(ctx.query.username,404,'username is required');
ctx.assert(ctx.query.password,404,'password is required');
ctx.body= ' success ';
})
app.use(router.routes());
app.listen(3000);複製代碼
ctx.state = 404複製代碼
ctx.redireact('/register');
ctx.redireact('http://www.baidu.com');複製代碼
koa不自帶static,須要安裝koa-staticsql
npm i koa-static複製代碼
const Koa = require('koa');
const Router = require('koa-router');
const static = require('koa-static');
let app = new Koa();
let router = new Router();
app.use(static('./static',{
maxage: 17*86400*100; //緩存時間
index: 'index.html'; //跟文件
})
app.use(router.routes());
app.listen(3000);複製代碼
const Koa=require('koa');
const Router=require('koa-router');
const static=require('koa-static');
let server=new Koa();
server.listen(8080);
//
let router=new Router();
router.get('/user', async ctx=>{
});
server.use(router.routes());
let staticRouter=new Router();
staticRouter.all(/(\.jpg|\.png|\.gif)$/i, static('./static', {
maxage: 60*86400*1000
}));
staticRouter.all(/(\.css)$/i, static('./static', {
maxage: 1*86400*1000
}));
staticRouter.all(/(\.html|\.htm|\.shtml)$/i, static('./static', {
maxage: 20*86400*1000
}));
staticRouter.all('', static('./static', {
maxage: 30*86400*1000
}));
server.use(staticRouter.routes());複製代碼
koa-better-body可以解析上傳的文件以及普通的post數據數據庫
const Koa=require('koa');
const Router=require('koa-router');
const body=require('koa-better-body');
let server=new Koa();
server.listen(8080);
server.use(body({
// 文件上傳的路徑
uploadDir: './static/upload'
}));
server.use(async ctx=>{
//文件和post數據信息都在ctx.request.fields裏面
console.log(ctx.request.fields);
ctx.body='aaa';
});
複製代碼
koa自帶cookienpm
const Koa=require('koa');
const Router=require('koa-router');
let server=new Koa();
server.listen(8080);
// 循環祕鑰
server.keys=[
'sdfasdfasdfsadfasddfsdaf',
'gutryurtyhfddyt8uytug',
'tyutryur687tyuyujghjkhj',
];
server.use(async ctx=>{
ctx.cookies.set('user', 'blue', {
signed: true
// 還能夠加httpOnly,domain,maxAge等等
});
console.log(ctx.cookies.get('user', {
// 校驗cookie
signed: true
}));
});
複製代碼
npm i koa-session複製代碼
const Koa=require('koa');
const Router=require('koa-router');
const session=require('koa-session');
let server=new Koa();
server.listen(8080);
// 循環祕鑰
server.keys=[
'asdfasdfasdfasdfasdf',
'hghjfgjghjkyggfytyurt',
'hjghjkfguig8ygyi8t78i8',
];
server.use(session({
maxAge: 20*60*1000, //有效期
renew: true //自動續期
}, server));
server.use(async ctx=>{
// session信息能夠在ctx.session中尋找
if(!ctx.session['view']){
ctx.session['view']=0;
}
ctx.session['view']++;
ctx.body=`歡迎你第${ctx.session.view}次來訪`;
});
複製代碼
database.js緩存
const mysql=require('mysql');
const co=require('co-mysql');
let conn=mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: '20181101'
});
let db=co(conn);
module.exports=db;
複製代碼
app.jsbash
const Koa=require('koa');
const Router=require('koa-router');
let server=new Koa();
server.listen(8080);
server.context.db=require('./libs/database');
server.use(async (ctx, next)=>{
try{
let data = await ctx.db.query('SELECT * FROM user');
console.log(data);
}catch(err){
console.log(err);
}
});
server.use(router.routes());複製代碼
const Koa=require('koa');
const Router=require('koa-router');
let server=new Koa();
server.listen(8080);
server.context.db=require('./libs/database');
/*
在全部的use以前,處理普通的錯誤,由於執行了next(),全部下面全部的錯誤都會在該處被捕獲
*/
server.use(async (ctx, next)=>{
try{
await next();
}catch(e){
ctx.body='錯了';
}
});
let router=new Router();
/*
在全部路由以前,處理路由錯誤,* 表示全部路由
*/
router.all('*', async ctx=>{
try{
await next();
}catch(e){
ctx.body='錯了-router';
}
});
router.get('/a', async ctx=>{
ctx.body=div.title;
});
server.use(router.routes());
複製代碼