当前位置:网站首页>[Nodejs] 20. Koa2 onion ring model ----- code demonstration
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
2022-07-06 12:22:00 【weiweicn!】
notes : Demo official website (https://koa.bootcss.com/) The code in 
- The code is as follows :
// demonstration koa2 Onion ring model of middleware
const Koa = require('koa');
const app = new Koa;
// logger ctx = req+res
app.use(async(ctx, next) => {
await next(); // Execute next Middleware
const rt = ctx.response.get('X-Response-Time') // res Get time difference
console.log(`${
ctx.method} ${
ctx.url} - ${
rt}`)
})
// x-response-time
app.use(async(ctx, next) => {
const start = Date.now();
await next(); // Execute next Middleware
const ms = Date.now()- start; // Calculate the time difference
ctx.set('X-Response-Time', `${
ms}ms`) // Record / Set the time difference
})
// response
app.use(async(ctx, next) => {
ctx.body = 'Hello world';
})
app.listen(3000);
console.log('koa2 Has started listening 3000 port ')
- Enter access in browser :


边栏推荐
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- Redis based distributed locks and ultra detailed improvement ideas
- Page performance optimization of video scene
- Priority inversion and deadlock
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- MySQL占用内存过大解决方案
- arduino获取数组的长度
- arduino获取随机数
- Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
- Understanding of AMBA, AHB, APB and Axi
猜你喜欢

Learning notes of JS variable scope and function

(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
![[esp32 learning-2] esp32 address mapping](/img/ee/c4aa0f7aed7543bb6807d7fd852c88.png)
[esp32 learning-2] esp32 address mapping

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)

Programmers can make mistakes. Basic pointers and arrays of C language

【ESP32学习-2】esp32地址映射

Time slice polling scheduling of RT thread threads

ES6语法总结--上篇(基础篇)

MySQL時間、時區、自動填充0的問題

The dolphin scheduler remotely executes shell scripts through the expect command
随机推荐
[Red Treasure Book Notes simplified version] Chapter 12 BOM
MySQL時間、時區、自動填充0的問題
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
[Offer29] 排序的循环链表
STM32 how to locate the code segment that causes hard fault
Vscode basic configuration
Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
Walk into WPF's drawing Bing Dwen Dwen
嵌入式启动流程
如何给Arduino项目添加音乐播放功能
Rough analysis of map file
Common DOS commands
Kconfig Kbuild
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Analysis of charging architecture of glory magic 3pro
Selective sorting and bubble sorting [C language]
Classification, understanding and application of common methods of JS array
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
Time slice polling scheduling of RT thread threads
JS数组常用方法的分类、理解和运用