0
0
Expressframework~20 mins

Single file upload in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when uploading a file with this Express setup?

Consider this Express route using multer middleware for single file upload:

const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();

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

If a client uploads a file named photo.png under the form field file, what will the server respond with?

Express
const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();

app.post('/upload', upload.single('file'), (req, res) => {
  res.send(req.file.originalname);
});
AThe path where the file is saved on the server
BThe string 'photo.png'
CAn error because req.file is undefined
DThe string 'file'
Attempts:
2 left
💡 Hint

Look at what req.file.originalname represents in multer.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets up multer for single file upload in Express?

Choose the correct code snippet to configure multer for handling a single file upload with field name avatar:

Aconst upload = multer({ dest: 'uploads/' }); app.post('/profile', upload.single('avatar'), (req, res) => { res.send('Uploaded'); });
Bconst upload = multer({ dest: 'uploads/' }); app.post('/profile', upload.array('avatar'), (req, res) => { res.send('Uploaded'); });
Cconst upload = multer({ dest: 'uploads/' }); app.post('/profile', upload.fields([{ name: 'avatar' }]), (req, res) => { res.send('Uploaded'); });
Dconst upload = multer({ dest: 'uploads/' }); app.post('/profile', upload.single(), (req, res) => { res.send('Uploaded'); });
Attempts:
2 left
💡 Hint

Remember the method to handle a single file upload requires the field name as an argument.

🔧 Debug
advanced
2:00remaining
Why does this Express file upload code cause an error?

Examine this code snippet:

const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();

app.post('/upload', (req, res) => {
  console.log(req.file);
  res.send('Done');
});

When a file is uploaded, req.file is undefined. Why?

Express
const express = require('express');
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const app = express();

app.post('/upload', (req, res) => {
  console.log(req.file);
  res.send('Done');
});
ABecause the destination folder 'uploads/' does not exist
BBecause the file field name is missing in the form data
CBecause the multer middleware is not used in the route to process the file
DBecause Express does not support file uploads
Attempts:
2 left
💡 Hint

Check if the middleware that processes the file is applied to the route.

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

Given this Express route:

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

If a user uploads a file named report.pdf, what key will NOT be present in req.file?

Express
app.post('/upload', upload.single('document'), (req, res) => {
  res.json(req.file);
});
Aoriginalname
Bfilename
Cpath
Dbuffer
Attempts:
2 left
💡 Hint

Consider multer's default storage when using dest option.

🧠 Conceptual
expert
3:00remaining
Which option best explains why multer's single file upload middleware must be placed before the route handler?

In Express, why must upload.single('file') be used as middleware before the route handler function?

ABecause multer middleware parses the incoming multipart/form-data and populates req.file before the handler runs
BBecause Express requires all middleware to be declared globally before any routes
CBecause the route handler cannot access req.body without multer middleware
DBecause multer middleware automatically sends the response and prevents the handler from running
Attempts:
2 left
💡 Hint

Think about what happens to the request data before your route code runs.