Building ApplicationsLesson 43Free preview
Middleware Concept
This lesson is part of Node.js · All levels
Summary
Middleware is a function that executes sequentially on each request before reaching the final handler. Each middleware receives req, res, and next so it can log, parse, validate, and authenticate. By dividing tasks into middleware chains, applications become modular and easy to test.
Key points
- Middleware is added with app.use and runs in sequence
- Call next() to forward the request to the next middleware
- If next(error) is called, a special middleware error will handle
- Express's in-house middleware includes express.json and express.static
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.