Middleware execution Mechanism of express and koa

const Koa = require("koa")
const route = require("koa-route")
const app = new Koa()

app.use(async function middleware1(ctx, next) {
  console.log("middleware1 ")
  await next()
  console.log("middleware1 ")
})

app.use(async function middleware2(ctx, next) {
  console.log("middleware2 ")
  await next()
  console.log("middleware2 ")
})

app.use(async function middleware3(ctx, next) {
  console.log("middleware3 ")
  await next()
  console.log("middleware3 ")
})

app.use(route.get("/", (ctx, next) => {
  ctx.body = "hello koa"
}))

app.listen(3001, function () {
  console.log("server is listening on port 3001")
})

middleware1 
middleware2 
middleware3 
middleware3 
middleware2 
middleware1 

this code uses Koa, to follow Koa"s "onion ring" middleware model, and the code in the middleware executes like a "paper clip" with the result as above.

const express = require("express")
const app = express()

app.use(function middleware1(req, res, next) {
  console.log("middleware1 ")
  next()
  console.log("middleware1 ")
})

app.use(function middleware2(req, res, next) {
  console.log("middleware2 ")
  next()
  console.log("middleware2 ")
})

app.use(function middleware3(req, res, next) {
  console.log("middleware3 ")
  next()
  console.log("middleware3 ")
})

app.get("/", function (req, res, next) {
  res.end("hello express")
})

app.listen(3000, function () {
  console.log("server listening on port 3000!")
})

middleware1 
middleware2 
middleware3 
middleware3 
middleware2 
middleware1 
The middleware model of

Express is linear, and the code after next () should not be executed, but the execution result of the above code is the same as using Koa.
I would like to ask the bosses to explain why the implementation result is like this.

Mar.11,2021

 

Express's middleware model is linear. Code after, next () should not execute . This is not true. Express is also an onion ring model, and next will transfer control to the next middleware.

the adopted answer, the implementation result of express should be consistent with that of koa.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c2c8-2c306.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c2c8-2c306.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?