Middleware เป็น framework ของ hook ที่ประมวลผลทุก request และ responseแบบ global โดยอยู่ระหว่าง web server และ view ของคุณ middleware แต่ละตัวสามารถตรวจสอบ/แก้ไข request ขาเข้าและ response ขาออกได้ — เหมาะสำหรับ concern แบบ cross-cutting เช่น authentication, security header, session และ logging
request/response flow ผ่าน middleware
Request → MW1 → MW2 → MW3 → VIEW
Response ← MW1 ← MW2 ← MW3 ← VIEW
Middleware wraps the view like layers of an onion:
- on the way IN (request): top-to-bottom
- on the way OUT (response): bottom-to-top (reverse order)
