koa2--04.ejs template engine

First used in the project file cmd, enter: npm install --save koa-views ejs, will be installed koa-views middleware and ejs template engine to a file, and automatically write dependence

Then inserted in the following code file index.js

const koa = require('koa');
const router = require('koa-router')();
views const = the require ( 'KOA-views' );
 var App = new new KOA ();
 // views (' the absolute path of a file folder placement engine ',' each of the paths in the file ejs into html files and render mapped to the engine '); 

app.use (views ( ' views' , {
    map:{
        html:'ejs'
    }
}));
// Example rendering index.ejs template folder views 
router.get ( '/', the async (CTX) => {
    await ctx.render('index.ejs');
});

app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);

 

Guess you like

Origin www.cnblogs.com/Liqian-Front-End-Engineer/p/11794229.html