文章标题 原创 翻译 转载 文章内容 # 场景 给我的博客增加显示用户访问量,后台的每个请求访问量都新增1,每个页面的渲染都返回当前访问量计数。 # 捕捉每次请求 在所有路由之前新增 ``` app.use(async (ctx, next) => { const totalhit = await Extends.addHit(ctx.path); ctx.state = Object.assign(ctx.state, { totalhit: totalhit }); await next(); }) ``` Extends.addHit写入mongodb新增访问量并且返回当前总访问量 # 每个请求中返回总访问量 还是在上面的代码中将总访问量totalhit赋值给ctx.state,由于state是一个对象,可能存在其他数据所以要使用Object.assign的方式将总访问量属性拷贝给state对象。 这样,每个渲染的html模板都可以获取totalhit变量。本博客使用的模板引擎是koa-swig。 # 前端实现 ``` <a href="http://3inns.cn" style="color:red;"> {% if totalhit %} 您是第{{ totalhit }}位访问者 {% else %} 3inns.cn {% endif %} </a> ``` 文章类别 Python Mobile Android Java Shell Life Database Bug Windows IOS Tools Boost Node.js Mac Product Tips C/C++ Golang Javascript React Qt MQ MongoDB Design Web Linux LLM ChatGPT RAG AI 提交