Web middleware
Web middleware is a function called before and after (partially). Middleware functions can access request and response objects.
Different upper-layer web frameworks have different middleware forms. Midway standard middleware is based on the onion ring model. Express, on the other hand, is a traditional queue model.
Koa and EggJs can be executed** before and after the **controller. In Express, the middleware can only be called before the controller, which will be introduced separately in Express chapters.
For the following code, we will take @midwayjs/koa
as an example.
Writing middleware
In general, we will write Web middleware in the src/middleware
folder.
Create a src/middleware/report.middleware.ts
. In this web middleware, we print the time when the controller (Controller) executes.
➜ my_midway_app tree
.
├── src
│ ├── controller
│ │ ├── user.controller.ts