Performance: File type validation
This affects server response time and user experience by preventing unnecessary file processing and reducing server load.
Jump into concepts and practice - no test required
app.post('/upload', (req, res) => { const file = req.files.file; const allowedTypes = ['image/png', 'image/jpeg']; if (!allowedTypes.includes(file.mimetype)) { return res.status(400).send('Invalid file type'); } processFile(file); res.send('File uploaded'); });
app.post('/upload', (req, res) => { const file = req.files.file; // No file type check processFile(file); res.send('File uploaded'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No file type validation | N/A | N/A | N/A | [X] Bad |
| Early file type validation | N/A | N/A | N/A | [OK] Good |
fileFilter in multer when handling file uploads in Express?fileFilter function is designed to check the file type before saving.fileFilter function in multer that only accepts PNG files?const upload = multer({
fileFilter: (req, file, cb) => {
if (file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(new Error('Only PNG files allowed'), false);
}
}
});fileFilter: (req, file, cb) => {
if (file.mimetype = 'application/pdf') {
cb(null, true);
} else {
cb(null, false);
}
}fileFilter function correctly implements this in Express using multer?