0
0
Expressframework~20 mins

File type validation in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Type Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when uploading a .txt file with this Express file validation?

Consider this Express middleware snippet that validates uploaded file types:

const multer = require('multer');
const upload = multer({
  fileFilter: (req, file, cb) => {
    if (file.mimetype === 'image/png' || file.mimetype === 'image/jpeg') {
      cb(null, true);
    } else {
      cb(new Error('Only PNG and JPEG allowed'));
    }
  }
});

If a user uploads a file named notes.txt, what will happen?

AThe upload is rejected and an error 'Only PNG and JPEG allowed' is triggered.
BThe upload is accepted and file is saved.
CThe upload is accepted but file is renamed to .png.
DThe upload is rejected with a generic server error.
Attempts:
2 left
💡 Hint

Check the fileFilter function and what happens when the mimetype is not allowed.

📝 Syntax
intermediate
2:00remaining
Which option correctly validates only PDF files in Express multer?

Choose the correct fileFilter function to accept only PDF files.

AfileFilter: (req, file, cb) => { if (file.mimetype === 'application/pdf') cb(null, true); else cb(null, false); }
BfileFilter: (req, file, cb) => { if (file.mimetype === 'pdf') cb(null, true); else cb(null, false); }
CfileFilter: (req, file, cb) => { if (file.mimetype === 'application/pdf') cb(null, true); else cb(new Error('Only PDFs allowed')); }
DfileFilter: (req, file, cb) => { if (file.type === 'application/pdf') cb(null, true); else cb(null, false); }
Attempts:
2 left
💡 Hint

Check the exact mimetype string and how errors are handled.

🔧 Debug
advanced
2:00remaining
Why does this Express file validation always accept files regardless of type?

Look at this fileFilter function:

fileFilter: (req, file, cb) => {
  if (file.mimetype === 'image/png' || 'image/jpeg') {
    cb(null, true);
  } else {
    cb(new Error('Invalid file type'));
  }
}

Why does it accept all files, even non-images?

ABecause the callback is never called.
BBecause the condition always evaluates to true due to incorrect logical OR usage.
CBecause the mimetype property is misspelled.
DBecause the error callback is missing.
Attempts:
2 left
💡 Hint

Look carefully at the condition inside the if statement.

state_output
advanced
2:00remaining
What is the value of req.file after uploading a valid PNG file?

Given this Express route using multer:

const upload = multer({
  fileFilter: (req, file, cb) => {
    if (file.mimetype === 'image/png') cb(null, true);
    else cb(new Error('Only PNG allowed'));
  }
});

app.post('/upload', upload.single('photo'), (req, res) => {
  res.json(req.file);
});

If a user uploads a valid PNG file named pic.png, what will req.file contain?

AAn object with file details including originalname, mimetype, and buffer.
BUndefined, because fileFilter does not save the file.
CAn error object with message 'Only PNG allowed'.
DAn empty object {}.
Attempts:
2 left
💡 Hint

Think about what multer does when the file passes the filter.

🧠 Conceptual
expert
3:00remaining
Which approach best prevents uploading dangerous file types in Express?

To protect your Express app from dangerous file uploads, which approach is most effective?

ARename all files to .txt to neutralize them.
BAllow all uploads and rely on client-side validation.
COnly check the file extension in the filename string.
DCheck the file mimetype and reject disallowed types in multer's fileFilter, then scan file content for malware.
Attempts:
2 left
💡 Hint

Think about both file type validation and security scanning.