Art-template is a minimalist, ultra-fast template engine. Tencent development and open source, speed performance is the best. NPM install –save koa-art-template –save koa-art-template let render = require(‘koa-art-template’); Render (app, {root: path.join(__dirname, ‘views’), extname: ‘.html’, debug: process.env.node_env! == ‘production’ }); Then use await ctx.render(‘user’); Others are fully compatible with ejS template syntax as well as EJS. PS: If path is used for the first time, the system may prompt that path does not exist. You need to introduce the path module, let path=require(“path”).

Cookies are variables stored on the visitor’s computer. It allows us to share data when accessing the same domain name using the same browser. In KOA, cookies do not need to be installed and can be used directly.

For example, in the index page, set a cookies router.get("/",async (ctx)=>{
    let zhi="Here's the data from JS."
    await ctx.render("index",{
       chuan:zhi
    });
    ctx.cookies.set("username"."zhaoyanwei",{maxAge:10*1000 // expiration time 10 seconds})}); Then you can go to other pages to obtain cookies, such as printing router.get("/news",async (ctx)=>{
    let list=[
        {title:"Redmi's official account announces March 18th.",time:"2018-12-24"},
        {title:"The biggest upgrade to the Redmi Note 7 Pro is the camera.",time:"2018-12-26"},
        {title:"You can take bright, clear night shots with your hands.",time:"2018-12-25"},
        {title:"Very competitive at the same price.",time:"2019-01-21"},]; await ctx.render("news",{
        list
    });
    var username = ctx.cookies.get("username"); Console.log (username)}) go to the root page and then to the news page, and you can see the printed username on the console. And ten seconds expired, ten seconds after the refresh will not print out. Browser cookies are stored in the application after F12, there is a cookie, you can see the cached data.Copy the code

Session is another mechanism for recording the client’s status. The difference is that cookies are stored in the client browser, while sessions are stored on the server. When the browser visits the server and sends the first request, the server will create a session object, generate a key-value pair similar to key and value, and then return the key(cookie) to the browser (client). The browser will carry the key(cookie) the next time it visits the server. Find the corresponding session(value). The client’s information is stored in the session. 1. Cookie data is stored in the client’s browser, and session data is stored on the server. 2. Cookies are not very secure. Others can analyze cookies stored locally and cheat cookies. 3. Sessions are stored on the server for a certain period of time. Cookies should be used to reduce server performance when the number of accesses increases. 4. The data saved by a single cookie cannot exceed 4K. Many browsers limit the maximum number of cookies saved by a site to 20.

NPM install koa-session –save let session = require(‘koa-session’);

App.keys = ['some secret hurr'];
const CONFIG = {
    key: 'koa:sess', maxAge: 86400000,// Session expiration time autoCommit:true,
    overwrite: true,
    httpOnly: true, / /trueIndicates that only the server side can get cookies signed:true,
    rolling: false,
    renew: false}; app.use(session(CONFIG, app)); Then you can use the setting ctx.session.infoname="Jay Chou"; Get the value ctx.session.infoname let's experiment by setting one on the login page and going to the About page to receive router.get("/login",async (ctx)=>{
    ctx.session.infoname="Jay Chou";
    await ctx.render("login");
});
router.get("/about",async (ctx)=>{
    await ctx.render("about");
    console.log(ctx.session.infoname);
});
Copy the code