0
0
Expressframework~5 mins

File type validation in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
In Multer's fileFilter function, what should you do to reject a file with an invalid type?
ACall callback(null, true)
BCall callback(new Error('Invalid file type'), false)
CThrow an exception
DReturn false
Which property of the uploaded file is commonly checked to validate its type?
Afile.size
Bfile.path
Cfile.mimetype
Dfile.name
Why should file type validation not rely only on the file extension?
AExtensions can be changed by users and may not reflect the real file type
BExtensions are always correct
CExtensions are checked by the browser
DExtensions are not sent in uploads
What happens if you do not validate file types on the server?
AUsers can upload harmful or unsupported files
BAll files are accepted safely
CFiles are automatically converted
DUploads are blocked
Explain how to implement file type validation in Express using Multer.
Think about how Multer lets you control which files to accept.
You got /4 concepts.
    Why is server-side file type validation important even if the client validates files?
    Consider what happens if a user ignores client checks.
    You got /4 concepts.