File type validation helps make sure users upload only allowed file types. This keeps your app safe and working well.
File type validation in Express
const multer = require('multer'); const fileFilter = (req, file, cb) => { if (file.mimetype === 'allowed/type') { cb(null, true); // accept file } else { cb(null, false); // silently reject file } }; const upload = multer({ fileFilter });
The fileFilter function checks the file's MIME type.
Call cb(null, true) to accept or cb(null, false) to silently reject.
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(null, false);
}
};const fileFilter = (req, file, cb) => {
const allowedTypes = ['application/pdf', 'application/msword'];
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(null, false);
}
};This Express app uses multer to accept file uploads at /upload. The fileFilter allows only JPEG and PNG images. If a user uploads a different file type, the server responds with an error message.
const express = require('express'); const multer = require('multer'); const app = express(); const fileFilter = (req, file, cb) => { if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') { cb(null, true); } else { cb(new Error('Only JPEG and PNG files are allowed'), false); } }; const upload = multer({ fileFilter }); app.post('/upload', upload.single('photo'), (req, res) => { res.send('File uploaded successfully'); }); app.use((err, req, res, next) => { if (err) { res.status(400).send(err.message); } else { next(); } }); app.listen(3000, () => { console.log('Server started on http://localhost:3000'); });
Always check file.mimetype to validate file types securely.
Handle errors from fileFilter to inform users about invalid files.
Combine file type validation with file size limits for better control.
File type validation ensures only allowed files are uploaded.
Use multer's fileFilter function to check file MIME types.
Provide clear error messages when users upload wrong file types.
Practice
fileFilter in multer when handling file uploads in Express?Solution
Step 1: Understand multer's fileFilter role
ThefileFilterfunction is designed to check the file type before saving.Step 2: Identify the purpose of fileFilter
It filters files by MIME type or extension to allow only certain types.Final Answer:
To allow only specific file types to be uploaded -> Option DQuick Check:
fileFilter controls allowed file types [OK]
- Confusing fileFilter with file renaming
- Thinking fileFilter compresses files
- Assuming fileFilter stores files in DB
fileFilter function in multer that only accepts PNG files?Solution
Step 1: Check multer fileFilter signature
The function receives (req, file, cb) and calls cb(error, acceptBoolean).Step 2: Validate correct callback usage
fileFilter: (req, file, cb) => { cb(null, file.mimetype === 'image/png'); } correctly calls cb with null error and true/false for acceptance based on mimetype.Final Answer:
fileFilter: (req, file, cb) => { cb(null, file.mimetype === 'image/png'); } -> Option AQuick Check:
fileFilter uses cb(null, boolean) [OK]
- Omitting the null error argument in callback
- Using wrong parameter order
- Checking file.type instead of file.mimetype
const upload = multer({
fileFilter: (req, file, cb) => {
if (file.mimetype === 'image/png') {
cb(null, true);
} else {
cb(new Error('Only PNG files allowed'), false);
}
}
});Solution
Step 1: Analyze fileFilter logic for 'image/jpeg'
The code only accepts 'image/png'; other types cause an error callback.Step 2: Determine multer behavior on error in fileFilter
Calling cb with an Error rejects the upload and triggers the error handler.Final Answer:
The upload fails with an error 'Only PNG files allowed' -> Option CQuick Check:
fileFilter error rejects upload [OK]
- Assuming non-PNG files are accepted
- Thinking files get renamed automatically
- Believing upload silently ignores invalid files
fileFilter: (req, file, cb) => {
if (file.mimetype = 'application/pdf') {
cb(null, true);
} else {
cb(null, false);
}
}Solution
Step 1: Check the if condition syntax
The code uses single = which assigns value instead of comparing.Step 2: Understand impact of assignment in condition
This causes the condition to always be true, accepting all files incorrectly.Final Answer:
Using assignment (=) instead of comparison (===) in the if condition -> Option BQuick Check:
Use === for comparison, not = [OK]
- Confusing = and === in conditions
- Omitting error argument in callback is allowed here
- Mixing up file.type and file.mimetype
fileFilter function correctly implements this in Express using multer?Solution
Step 1: Check correct MIME type validation
fileFilter: (req, file, cb) => { const allowed = ['image/png', 'image/jpeg', 'application/pdf']; if (allowed.includes(file.mimetype)) { cb(null, true); } else { cb(new Error('Invalid file type. Only PNG, JPEG, PDF allowed'), false); } } uses an array and includes() to check if file.mimetype matches allowed types.Step 2: Verify error handling and message clarity
fileFilter: (req, file, cb) => { const allowed = ['image/png', 'image/jpeg', 'application/pdf']; if (allowed.includes(file.mimetype)) { cb(null, true); } else { cb(new Error('Invalid file type. Only PNG, JPEG, PDF allowed'), false); } } calls cb with an Error and false to reject invalid types with a clear message.Final Answer:
fileFilter function that checks allowed MIME types array and returns error with message -> Option AQuick Check:
Use array.includes and error callback for validation [OK]
- Using || incorrectly without repeating comparisons
- Using && which requires all types at once (impossible)
- Checking file.extension which is not a multer property
Ready to prove it?
Pass the test and this pattern moves unknown
