📖 Scenario: You are building a simple web server using Express. Middleware functions are the building blocks that handle requests and responses step-by-step, like a team passing a message along a chain.
🎯 Goal: Build a basic Express server that uses middleware to log requests, check a condition, and send a response.
📋 What You'll Learn
Create an Express app variable
Add a middleware function to log each request's method and URL
Add a middleware function to check if a query parameter allow equals true
Send a response with Access granted if allowed, otherwise Access denied
💡 Why This Matters
🌍 Real World
Middleware is used in real web servers to handle logging, authentication, data parsing, and more in a clean, organized way.
💼 Career
Understanding middleware is essential for backend developers working with Express or similar web frameworks to build scalable and maintainable web applications.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express by requiring the 'express' module, then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and then call it as a function to create the app.
2
Add logging middleware
Add a middleware function to app using app.use that logs the HTTP method and URL of each request using console.log. Use parameters req, res, and next. Call next() at the end.
Express
Hint
Middleware functions take three arguments and must call next() to pass control.
3
Add access control middleware
Add another middleware function to app using app.use that checks if req.query.allow equals the string 'true'. If yes, call next() to continue. Otherwise, send a response with Access denied and do not call next().
Express
Hint
Check the query parameter and decide whether to continue or send a response.
4
Add final response handler
Add a middleware function to app using app.use that sends the response Access granted. This will run only if previous middleware called next().
Express
Hint
This middleware sends the final response if access is allowed.
Practice
(1/5)
1. Why is middleware considered the core concept in Express?
easy
A. Because it allows adding reusable functions to process requests and responses
B. Because it automatically generates HTML pages
C. Because it replaces the need for routing in Express
D. Because it stores data permanently in the database
Solution
Step 1: Understand middleware role
Middleware functions run during request processing to handle tasks like logging, authentication, or parsing.
Step 2: Identify middleware's core purpose
It allows adding reusable steps that can modify requests or responses before final handling.
Final Answer:
Because it allows adding reusable functions to process requests and responses -> Option A
Quick Check:
Middleware = reusable request handlers [OK]
Hint: Middleware = reusable steps for requests [OK]
Common Mistakes:
Thinking middleware generates HTML automatically
Confusing middleware with database storage
Believing middleware replaces routing
2. Which of the following is the correct syntax to use middleware in Express?
easy
A. app.route(function(req, res, next) { next(); });
B. app.middleware(function(req, res) { res.send('Hi'); });
C. app.use(function(req, res, next) { next(); });
D. app.handle(function(req, res) { next(); });
Solution
Step 1: Recall Express middleware syntax
Middleware is added using app.use() with a function that takes req, res, next.
Step 2: Match syntax to options
Only app.use(function(req, res, next) { next(); }); uses app.use and the correct function signature.
Final Answer:
app.use(function(req, res, next) { next(); }); -> Option C
Quick Check:
Middleware added with app.use() [OK]
Hint: Middleware uses app.use() with (req, res, next) [OK]
Common Mistakes:
Using app.middleware instead of app.use
Omitting the next parameter in middleware
Confusing app.route with middleware
3. What will be the output when this middleware is used?