Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is file type validation in Express?
File type validation in Express means checking the type of a file uploaded by a user to make sure it matches allowed formats before saving or processing it.
Click to reveal answer
beginner
Which middleware is commonly used in Express to handle file uploads and validate file types?
Multer is a popular middleware for handling file uploads in Express. It can be configured to validate file types by checking the file's mimetype or extension.
Click to reveal answer
intermediate
How can you reject files with disallowed types using Multer?
You can use Multer's fileFilter option, a function that receives the file and a callback. Inside, check the file's mimetype or extension. If allowed, call callback(null, true); if not, call callback(new Error('Invalid file type'), false).
Click to reveal answer
beginner
Why is it important to validate file types on the server side?
Validating file types on the server protects your app from harmful files, prevents unexpected errors, and ensures users upload only supported formats.
Click to reveal answer
intermediate
What is a simple example of a fileFilter function in Multer that only allows PNG and JPEG images?
A fileFilter function checks if file.mimetype is 'image/png' or 'image/jpeg'. If yes, it accepts the file; otherwise, it rejects it with an error.
Click to reveal answer
Which Express middleware is best suited for handling file uploads and validating file types?
ABody-parser
BHelmet
CCors
DMulter
✗ Incorrect
Multer is designed for handling multipart/form-data, which is used for file uploads, and supports file type validation.
In Multer's fileFilter function, what should you do to reject a file with an invalid type?
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.
B. Using assignment (=) instead of comparison (===) in the if condition
C. Incorrect parameter order in fileFilter function
D. Using file.type instead of file.mimetype
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 B
Quick Check:
Use === for comparison, not = [OK]
Hint: Use === for comparison, not = assignment [OK]
Common Mistakes:
Confusing = and === in conditions
Omitting error argument in callback is allowed here
Mixing up file.type and file.mimetype
5. You want to allow users to upload only images (PNG, JPEG) and PDFs, and provide a clear error message if the file type is invalid. Which fileFilter function correctly implements this in Express using multer?
hard
A. 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);
}
}