Performance: Admin vs user route protection
MEDIUM IMPACT
This affects server response time and user experience by controlling access efficiently and avoiding unnecessary processing.
function adminOnly(req, res, next) {
if (!req.user || req.user.role !== 'admin') {
return res.status(403).send('Forbidden');
}
next();
}
app.use('/admin', adminOnly);
app.get('/admin/dashboard', (req, res) => {
// heavy data processing
res.send('Admin Dashboard');
});app.get('/admin/dashboard', (req, res) => { // heavy data processing if (!req.user || req.user.role !== 'admin') { return res.status(403).send('Forbidden'); } else { res.send('Admin Dashboard'); } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Authorization in route handler after processing | N/A (server-side) | N/A | N/A | [X] Bad |
| Authorization middleware before route handler | N/A (server-side) | N/A | N/A | [OK] Good |