1)新建文件夹
mkdir koademo
cd koademo
npm init -y //初始化包
2)安装koa
npm install --save koa
3)获得get请求的方法
第一种:
const Koa = require('koa');
const app = new Koa();
app.use(async ctx=>{
let {request} =ctx;
let req_query = request.query;
ctx.body=req_query
})
app.listen(3030,()=>{
console.log('server is running...')
})
第二种:
const Koa = require('koa');
const app = new Koa();
app.use(async ctx=>{
let ctx_requery = ctx.query;
let ctx_querystring = ctx.querystring;
ctx.body=req_query
})
app.listen(3030,()=>{
console.log('server is running...')
})
4)koa获得post请求参数
1)安装中间插件 koa-bodyparser
npm install --save koa-bodyparser@3
2)使用
const bodyparser = require(‘koa-bodyparser’);
app.use(bodyparser)
3)可以直接使用ctx.request.body获得post请求参数
const Koa = require('koa');
const app =new Koa();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser)
app.use(async ctx=>{
let result = ctx.request.body;
ctx.body=result;
})
app.listen(3030,()=>{
console.log('server is running...')
})
4)koa路由
1)安装路由中间件
npm install --save koa-router
2)使用
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
const router = new Router();
router
.get('/',(ctx,next)=>{
ctx.body='hello koa';
})
.get('/todo',(ctx,next)=>{
ctx.body="hello wodo"
})
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(3030,()=>{
console.log('server is running...')
})
- 路由设置层级
1)全局设置(/todo->/hyx/todo);
const router = new Router({
prefix:'/hyx'
})
访问:127.0.0.1:3030/hyx/todo
2)单页面设置
const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
let home = new Router();
home
.get('/todo',(ctx,next)=>{
ctx.body='这是home下的todo 页面'
})
.get('/list',(ctx,next)=>{
ctx.body='这是home下的list 页面'
})
let about = new Router();
about
.get('/list',(ctx,next)=>{
ctx.body = '这是about下的list 页面'
})
.get('/detail',(ctx,next)=>{
ctx.body ='这是about下的detial 页面'
})
let router = new Router();
router.use('/home',home.routes(),home.allowedMethods());
router.use('/about',about.routes(),about.allowedMethods());
app.use(router.routes()).use(router.allowedMethods());
app.listen(3030,()=>{
console.log('server is running ...')
})
koa中使用cookie
ctx.cookies.set(key,value,{
domain:'写入cookie所在的域名'
path:写入cookie的路径
maxAge:cookie最大有效时长
expires:cookie失效时间
httpOnly:是否只用http请求中获得
overwrite:是否允许重写
}) //设置
ctx.cookies.get(key)
koa中使用模板机制
1)安装
npm install --save koa-views
npm install --save ejs (ejs模板引擎)
2)使用
在根目录下新建views文件,在views中新建index.ejs
const Koa = require('koa');
const views = require('koa-views');
const path = require('path');
const app =new Koa();
app.use(views(path.join(__dirname,'./views'),{
extension:'ejs'
}))
app.use(async ctx=>{
let title = 'koa';
await ctx.render('index',{title})
})
app.listen(3030,()=>{
console.log('server is running...')
})
Koa使用静态资源
1)安装插件
npm install --save koa-staic
2)使用
const Koa = require('koa');
const path = require('path');
const static = require('koa-static');
const app = new Koa();
const staticPath = './static';
app.use(static(path.join(__dirname,staticPath)));
app.use(async ctx=>{
ctx.body='hello world'
})
app.listen(3030,()=>{
console.log('server is running...')
})