0
0
Expressframework~20 mins

res.download for file downloads in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Download Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when res.download is called with a valid file path?

Consider this Express route handler:

app.get('/file', (req, res) => {
  res.download('/files/report.pdf');
});

What is the expected behavior when a client requests /file?

Express
app.get('/file', (req, res) => {
  res.download('/files/report.pdf');
});
AThe server responds with a JSON object containing the file path.
BThe server sends the file content inline, displaying it in the browser if supported.
CThe server throws an error because res.download requires a callback.
DThe server sends the file 'report.pdf' as an attachment prompting the browser to download it.
Attempts:
2 left
💡 Hint

Think about what res.download is designed to do with files.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses res.download with a custom filename?

You want to send a file located at /files/data.csv but want the browser to save it as export.csv. Which code snippet does this correctly?

Ares.download('/files/data.csv', 'export.csv');
Bres.download('export.csv', '/files/data.csv');
Cres.download('/files/data.csv').filename('export.csv');
Dres.download('/files/data.csv', { filename: 'export.csv' });
Attempts:
2 left
💡 Hint

Check the order of parameters in res.download.

🔧 Debug
advanced
2:00remaining
Why does this res.download call cause an error?

Look at this code snippet:

app.get('/download', (req, res) => {
  res.download('/files/missing.pdf');
});

The file missing.pdf does not exist. What error will Express send to the client?

Express
app.get('/download', (req, res) => {
  res.download('/files/missing.pdf');
});
AA 404 Not Found error because the file does not exist.
BA 500 Internal Server Error because res.download crashes.
CNo error; the server sends an empty response.
DA 403 Forbidden error due to file permissions.
Attempts:
2 left
💡 Hint

Think about how Express handles missing files in res.download.

state_output
advanced
2:00remaining
What is the effect of providing a callback to res.download?

Consider this code:

app.get('/file', (req, res) => {
  res.download('/files/report.pdf', (err) => {
    if (err) {
      console.error('Download failed:', err);
      res.status(500).send('Error occurred');
    }
  });
});

What happens if the file is successfully sent?

Express
app.get('/file', (req, res) => {
  res.download('/files/report.pdf', (err) => {
    if (err) {
      console.error('Download failed:', err);
      res.status(500).send('Error occurred');
    }
  });
});
AThe callback is called with an error even if the download succeeds.
BThe file is sent and the callback is called with no error, so nothing else happens.
CThe callback is not called if the download succeeds.
DThe server sends the file twice, once normally and once in the callback.
Attempts:
2 left
💡 Hint

Callbacks in res.download are called after the transfer attempt.

🧠 Conceptual
expert
2:00remaining
Which statement about res.download is true?

Choose the correct statement about res.download in Express.

A<code>res.download</code> streams the file inline without changing headers.
B<code>res.download</code> requires manual setting of <code>Content-Type</code> header before calling.
C<code>res.download</code> automatically sets the <code>Content-Disposition</code> header to attachment with the filename.
D<code>res.download</code> only works with absolute file paths, not relative.
Attempts:
2 left
💡 Hint

Think about what Content-Disposition header does for downloads.