when learning express and koa middleware, I have always understood it according to the onion model. But in theory, they are different.
//express
var express = require("express");
var app = express();
app.use((req, res, next) => {
setTimeout(() => {
console.log("start1")
})
next();
console.log("end1")
})
app.use((req, res, next) => {
console.log("start2")
next();
console.log("end2");
})
app.use((req, res, next) => {
console.log("start3");
next();
console.log("end3");
})
app.get("/", function (req, res) {
console.log("before")
res.send("Hello World!");
console.log("after")
});
app.listen(3000, function () {
console.log("Example app listening on port 3000!");
});
koa version
const Koa = require("koa");
const app = new Koa();
app.use(async (ctx, next) => {
await new Promise((resolve, reject) => {
setTimeout(() => {
console.log("start1")
resolve()
})
})
await next();
console.log("end1")
})
app.use(async (ctx, next) => {
console.log("start2")
await next();
console.log("end2");
})
app.use(async (ctx, next) => {
console.log("start3");
await next();
console.log("end3");
})
app.use(ctx => {
console.log("before");
ctx.body = "Hello Koa";
console.log("after");
});
app.listen(3001)
the question now is, how can modify the above code to clearly tell the difference between express and koa middleware?