0
0
Expressframework~10 mins

File type validation in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Express module.

Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bhttp
Cfs
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express'.
Forgetting to put the module name in quotes.
2fill in blank
medium

Complete the code to use multer middleware for file uploads.

Express
const multer = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bcors
Cbody-parser
Dmulter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'body-parser' which does not handle file uploads.
Using 'express' instead of 'multer'.
3fill in blank
hard

Fix the error in the file filter function to only accept PNG files.

Express
const fileFilter = (req, file, cb) => {
  if (file.mimetype === '[1]') {
    cb(null, true);
  } else {
    cb(null, false);
  }
};
Drag options to blanks, or click blank then click option'
Aimage/png
Bapplication/pdf
Cimage/jpeg
Dtext/plain
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'image/jpeg' which is for JPEG images.
Using 'application/pdf' which is for PDF files.
4fill in blank
hard

Fill both blanks to configure multer with a file size limit and file filter.

Express
const upload = multer({
  limits: { fileSize: [1] },
  fileFilter: [2]
});
Drag options to blanks, or click blank then click option'
A1024 * 1024
BfileFilter
C5000
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Setting fileSize to 5000 which is too small.
Passing 'true' instead of the fileFilter function.
5fill in blank
hard

Fill all three blanks to create an Express route that uses multer to upload a single file named 'avatar'.

Express
app.post('/upload', [1].single('[2]'), (req, res) => {
  if (!req.file) {
    return res.status(400).send('[3]');
  }
  res.send('File uploaded successfully');
});
Drag options to blanks, or click blank then click option'
Aupload
Bavatar
CNo file uploaded
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of the multer instance.
Using a wrong field name other than 'avatar'.
Not handling the case when no file is uploaded.