Middleware Basics Prompts
AI prompts for middleware basics from the LearnWithHasan AI Coding Building Blocks (Architecture).
Add a Simple Login Check
Start here - simplest middleware pattern From the Middleware Basics AI Coding Building Block.
Create simple middleware that checks if a user is logged in before they can access protected pages. Framework: [Express, Flask, Django, FastAPI, etc.] Keep it simple: 1. Check if the user has a valid session or token 2. If yes: let them through to the page 3. If no: redirect to login page (or return 401 error for APIs) I want to understand the basic pattern first. Show me: - The middleware function itself (keep it short!) - How to apply it to routes that need login - How to skip it for public pages (like homepage, login page) I'm learning, so explain each part simply.
Log Every Request
Great second example - see every request From the Middleware Basics AI Coding Building Block.
Create simple middleware that logs every request that comes into my app. Framework: [Express, Flask, Django, FastAPI, etc.] For each request, log: 1. When it happened (timestamp) 2. What page they requested (URL/path) 3. How they requested it (GET, POST, etc.) Keep the code simple. I just want to understand how middleware can see every request. Show me where to put this middleware so it runs on ALL requests. I'm learning, so explain each part simply.
Explain My Existing Middleware
Understand existing code From the Middleware Basics AI Coding Building Block.
I have some middleware code but I don't fully understand what it's doing. Please explain it to me in simple terms. Here's the middleware: [paste your middleware code here] Please explain: 1. What does this middleware do? (one sentence) 2. Walk through it line by line. What happens at each step? 3. When does this middleware run? (all requests? specific routes?) 4. What happens if it "passes" vs "blocks" the request? 5. Are there any gotchas or common mistakes with this pattern? I'm learning, so explain each part simply.