第一次加载时,它显示正确。之后,每次更新都会获得 2 个页面浏览量,而不是一个。
const Koa = require('koa');
const session = require('koa-session');
const app = new Koa();
app.keys = ['Shh, its a secret!'];
app.use(session(app));
app.use(async function(ctx) {
let n = ctx.session.views || 0;
ctx.session.views = ++n;
console.log(`times= ${n}`);
if (n === 1) {
ctx.body = 'Welcome here for the first time!';
} else {
ctx.body = `You visited this page ${n} times!`;
}
});
app.listen(3000);