Because CTX itself is not render method, sometimes need to write a middleware to expand a method, the method is as follows

Const render=require(“./render”); App.use (render({dir:”template”}));

Const nunjucks = require(“nunjucks”); //const fs=require(“fs”) Using nunjucks template engine reconstruction following the module exports = (opts = {}) = > {nunjucks. Configure (opts. Dir | | “template”, {watch: Opts. Watch | | true, noCache: opts. NoCache | | true}) / / specify a folder

return async (ctx, next) => { ctx.render = function (... args) { ctx.body = nunjucks.render(... args); } await next(); }Copy the code

}

Create a project folder. 2. Initialize a project using NPM I koa koa-router koa-static-cache 6. Add “start” to scripts in package.json: 7, in app.js, a const koa =require(“koa”); App.js const KouRouter =require(“koa-router”); Create an app in app.js const app=new Koa(); Listen to app.js (888, Use (KoaStaticCache({prefix: =>{console.log(” server started successfully “)}) “/ /public”,// set the access prefix dir:”./public”,// set the directory that is read when accessed with the above visa dynamic:true, gzip:true

}))