day23

1-使用 koa 搭建服务器
  const koa = require('koa')
  const app = new koa()// new koa()创建服务
  // 路由请求
  // context    上下文    包含req和res
  app.use(async (ctx)=>{
    ctx.body = 'hello koa2'
  })
  app.listen(3000)
2-如何配置 koa 路由
  const koa = require('koa')
  var router = require('koa-router')()
  const app = new koa()
  // 路由请求
  router.get('/index',(cxt)=>{
    cxt.body = 'hello index'
  })
  router.get('/list',(cxt)=>{
    cxt.body = 'hello list'
  })
  // 启用路由
  app.use(router.routes())
  .use(router.allowedMethods())
  app.listen(3000)
  console.log('3000...')
3-静态资源如何获取
  app.use(static(path.join(__dirname,'public')))
4-koa 如何使用模板引擎
  const koa = require('koa')
  var router = require('koa-router')()
  const render = require('koa-art-template')
  const path = require('path')
  const app = new koa()
  render(app,{
    root:path.join(__dirname,'views'),
    extname:'.html',
    debug: process.env.NODE_ENV !== 'production'
  })
  router.get('/add',async (ctx) => {
    ...
  })
  app.use(router.routes())
  .use(router.allowedMethods())
  app.listen(3000)
5-使用 koa 实现一个用户增删改查的案例
posted @ 2021-03-05 15:35  qwerfd  阅读(73)  评论(0)    收藏  举报