📖 Scenario: You are building a simple Flask web app that needs to log every request's path before processing it.This helps you understand how middleware can intercept requests to add extra behavior.
🎯 Goal: Create a custom middleware in Flask that logs the request path for every incoming request.
📋 What You'll Learn
Create a Flask app instance named app
Define a middleware function named log_request_path
Use the middleware to log the request path before each request
Add a simple route /hello that returns 'Hello, World!'
💡 Why This Matters
🌍 Real World
Middleware is used in web apps to add features like logging, authentication, or request modification without changing route code.
💼 Career
Understanding middleware is important for backend developers working with Flask or similar web frameworks to build scalable and maintainable web services.
Progress0 / 4 steps
1
Create the Flask app instance
Create a Flask app instance called app using Flask(__name__).
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Define the middleware function
Define a function called log_request_path that takes no arguments and logs the current request path using print(request.path). Import request from flask.
Flask
Hint
Import request and define a function that prints request.path.
3
Register the middleware with before_request
Use @app.before_request decorator to register the log_request_path function as middleware that runs before each request.
Flask
Hint
Use @app.before_request decorator to add the middleware function.
4
Add a simple route to test
Add a route /hello using @app.route('/hello') that returns the string 'Hello, World!'.
Flask
Hint
Use @app.route('/hello') decorator and define a function that returns the greeting.
Practice
(1/5)
1. What is the main purpose of creating custom middleware in a Flask application?
easy
A. To run code before and after each request is processed by Flask
B. To define new routes and views in the Flask app
C. To replace Flask's built-in request handling completely
D. To create database models for the application
Solution
Step 1: Understand middleware role
Middleware runs code around request processing, before and after Flask handles requests.
Step 2: Compare options
Only To run code before and after each request is processed by Flask describes this behavior. Other options describe unrelated tasks.
Final Answer:
To run code before and after each request is processed by Flask -> Option A
Quick Check:
Middleware purpose = run code around requests [OK]
Hint: Middleware wraps requests to add extra processing [OK]
Common Mistakes:
Confusing middleware with route creation
Thinking middleware replaces Flask core
Mixing middleware with database models
2. Which of the following is the correct way to define a custom middleware class in Flask?
easy
A. class MyMiddleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return self.app(environ, start_response)
B. def MyMiddleware(app):
return app
C. class MyMiddleware:
def handle_request(self):
pass
D. class MyMiddleware:
def __init__(self):
pass
Solution
Step 1: Check middleware class structure
Middleware must accept the app in __init__ and implement __call__ with environ and start_response.
Step 2: Validate options
Only class MyMiddleware:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
return self.app(environ, start_response) matches this pattern correctly. Others miss __call__ or parameters.
Final Answer:
Correct middleware class with __init__ and __call__ methods -> Option A
Quick Check:
Middleware class needs __call__ method [OK]
Hint: Middleware class must implement __call__(environ, start_response) [OK]
Common Mistakes:
Omitting __call__ method
Not passing app to __init__
Using function instead of class
3. Given this middleware code, what will be printed when a request is made?