Performance: express.static middleware
MEDIUM IMPACT
This affects how quickly static files like images, CSS, and JavaScript are served to the browser, impacting page load speed.
app.use('/static', express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
if (req.url.startsWith('/static/')) {
const filePath = path.join(__dirname, 'public', req.url.replace('/static/', ''));
fs.readFile(filePath, (err, data) => {
if (err) return next();
res.send(data);
});
} else {
next();
}
});| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual file read per request | N/A | N/A | Increases server response delay | [X] Bad |
| express.static middleware | N/A | N/A | Fast response, enables browser caching | [OK] Good |