📖 Scenario: You are creating a simple Express server that logs request details for every incoming request. This helps you monitor what requests your server receives.
🎯 Goal: Build an Express app that uses application-level middleware to log the HTTP method and URL of each request before sending a response.
📋 What You'll Learn
Create an Express app instance
Add application-level middleware that logs request method and URL
Add a route handler for GET requests to '/'
Send a simple text response from the route handler
💡 Why This Matters
🌍 Real World
Logging requests helps developers monitor and debug web servers in real time.
💼 Career
Understanding middleware is essential for backend developers working with Express to build scalable web applications.
Progress0 / 4 steps
1
Create Express app instance
Write code to import Express and create an app instance called app.
Express
Hint
Use require('express') to import Express and call it to create app.
2
Add application-level middleware
Add application-level middleware to app using app.use that logs the HTTP method and URL of each request. Use parameters req, res, and next. Call next() after logging.
Express
Hint
Use app.use with a function that logs req.method and req.url, then calls next().
3
Add GET route handler for '/'
Add a GET route handler on app for path '/' that sends the text response 'Hello, world!'. Use parameters req and res.
Express
Hint
Use app.get with path '/' and send 'Hello, world!' in the response.
4
Start the server on port 3000
Add code to start the Express server by calling app.listen on port 3000. Provide a callback that logs 'Server running on port 3000'.
Express
Hint
Use app.listen(3000, () => { console.log(...) }) to start the server.
Practice
(1/5)
1. What is the main purpose of application-level middleware in Express?
easy
A. To run code for every request or specific routes before the final handler
B. To directly send HTML files to the client
C. To connect Express with a database
D. To create new HTTP servers
Solution
Step 1: Understand middleware role
Application-level middleware runs before route handlers to process requests.
Step 2: Identify middleware usage
It can modify requests, responses, or decide to stop the request cycle early.
Final Answer:
To run code for every request or specific routes before the final handler -> Option A
Quick Check:
Middleware runs before handlers [OK]
Hint: Middleware runs before routes to process requests [OK]
Common Mistakes:
Confusing middleware with route handlers
Thinking middleware creates servers
Assuming middleware connects databases directly
2. Which of the following is the correct syntax to add application-level middleware in Express?
easy
A. app.addMiddleware(function(req, res) { next(); });
B. app.middleware((req, res) => { console.log('Middleware'); });
C. app.use((req, res, next) => { console.log('Middleware'); next(); });
D. app.routeMiddleware((req, res, next) => { next(); });
Solution
Step 1: Recall Express middleware syntax
Express uses app.use() to add middleware functions.
Step 2: Check function parameters
Middleware functions receive req, res, next and must call next() to continue.