1. Koa
-
安装koa:
npm i koa -s
,下同const Koa = require("koa") // 安装好之后,引入模块,下同 const app = new Koa()
-
使用koa
app.use(static(__dirname, './img/img.png')) app.use(router.routes()) app.listen(1234)
2.Koa-router:路由
-
安装koa-router:
const Router = require("koa-router") const router = new Router()
-
使用:
router.get("/index", (ctx,next) => { ctx.body = $index.html() //使用了cheerio模块 console.log(data) })
3.Koa-static:加载静态文件
可通过localhost:1234/img.png访问
-
安装koa-static
const static = require('koa-static')
-
使用:
app.use(static(__dirname, './img/img.png'))
4.Koa-view:渲染模板
-
安装koa-view
const view = require('koa-views')
-
使用:
app.use(view(__dirname+'/view'),{ map:{ html: '模板引擎名称' } }) router.get('/index', async ctx =>{ await ctx.render("index", { newsData:newData, // 传入模板中的变量 totalPage, p }) })