šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

⚔ Total XP: 0|šŸ’» backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Applying middleware-level automatic caching to a route that returns per-user data

// Wrong: caches user A's private data and serves it to user B router.get('/orders/me', cacheMiddleware(300), ordersController); // Correct: key includes the user, or skip caching entirely for private data const key = `orders:${req.user.id}`; const cached = await redis.get(key);

The Solution //

A cacheMiddleware keyed only on req.url will cache and replay one user's private data (e.g. their own order history) to every other user who hits the same URL, because the cache key doesn't account for the authenticated user. Automatic URL-based caching is only safe for genuinely public, identical-for-everyone responses — for per-user data, include the user ID in the cache key or use manual caching with an explicit key.

The Error //

Overriding res.send in middleware but forgetting to preserve the original 'this' binding or status code

// Wrong: caches error responses too res.send = (body) => { redis.set(req.url, body); originalSend(body); }; // Correct: only cache success responses, preserve context res.send = function (body) { if (res.statusCode === 200) redis.set(req.url, body); return originalSend.call(this, body); };

The Solution //

A naive res.send override that ignores the response status code will happily cache and replay a 500 error response as if it were a successful 200, or lose the original 'this' context and throw at runtime. Only cache successful responses (check res.statusCode before saving) and call the original send with the correct context/arguments preserved.

Continue Learning