Performance: Protecting routes with auth middleware
MEDIUM IMPACT
This affects the server response time and user experience by adding authentication checks before route handlers run.
function authMiddleware(req, res, next) {
if (!req.user) {
return res.redirect('/login');
}
next();
}
app.get('/dashboard', authMiddleware, (req, res) => {
res.send('Dashboard');
});app.get('/dashboard', (req, res) => { if (!req.user) { res.redirect('/login'); return; } // route logic res.send('Dashboard'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Auth check inside each route | N/A (server-side) | N/A | N/A | [X] Bad |
| Auth middleware before routes | N/A (server-side) | N/A | N/A | [OK] Good |