Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does res.download() do in an Express app?
easy
A. Uploads a file from the user to the server
B. Sends a file to the user for download
C. Deletes a file on the server
D. Reads a file and logs its content

Solution

  1. Step 1: Understand the purpose of res.download

    res.download() is used to send files to users so they can download them easily.
  2. Step 2: Compare with other file operations

    Uploading, deleting, or reading files are different operations and not what res.download() does.
  3. Final Answer:

    Sends a file to the user for download -> Option B
  4. Quick Check:

    res.download() = Sends file [OK]
Hint: Remember: download means sending file to user [OK]
Common Mistakes:
  • Confusing download with upload
  • Thinking it deletes files
  • Assuming it reads file content
2. Which is the correct syntax to send a file named report.pdf for download using res.download?
easy
A. res.download('report.pdf')
B. res.download(report.pdf)
C. res.download('report.pdf', 'download')
D. res.download('report.pdf', {filename: 'download.pdf'})

Solution

  1. Step 1: Check the basic usage of res.download

    The first argument is the file path as a string, so res.download('report.pdf') is correct.
  2. Step 2: Analyze other options

    res.download(report.pdf) misses quotes making it invalid syntax. res.download('report.pdf', 'download') adds a second argument incorrectly as a string. res.download('report.pdf', {filename: 'download.pdf'}) uses an object which is not valid for filename.
  3. Final Answer:

    res.download('report.pdf') -> Option A
  4. Quick Check:

    File path string only = res.download('report.pdf') [OK]
Hint: File path must be a string in quotes [OK]
Common Mistakes:
  • Omitting quotes around filename
  • Passing wrong argument types
  • Using object instead of string for filename
3. What will happen when this code runs?
app.get('/file', (req, res) => {
  res.download('files/data.txt', 'info.txt');
});
medium
A. The file 'files/data.txt' is sent and saved as 'info.txt' by the user
B. The file 'info.txt' is sent from the server
C. An error occurs because the second argument is invalid
D. The file 'data.txt' is sent without renaming

Solution

  1. Step 1: Understand res.download parameters

    The first argument is the path on the server, the second is the filename for the user.
  2. Step 2: Apply to given code

    The server sends 'files/data.txt' but the user will see it saved as 'info.txt'.
  3. Final Answer:

    The file 'files/data.txt' is sent and saved as 'info.txt' by the user -> Option A
  4. Quick Check:

    res.download(path, filename) renames file [OK]
Hint: Second argument renames file for user [OK]
Common Mistakes:
  • Thinking second argument is server file path
  • Assuming no renaming happens
  • Believing second argument causes error
4. Identify the error in this code snippet:
app.get('/download', (req, res) => {
  res.download();
});
medium
A. res.download requires a callback as first argument
B. res.download cannot be used inside app.get
C. No error, code works fine
D. Missing file path argument in res.download

Solution

  1. Step 1: Check res.download required arguments

    res.download() requires at least the file path as the first argument.
  2. Step 2: Analyze given code

    The code calls res.download() with no arguments, causing an error.
  3. Final Answer:

    Missing file path argument in res.download -> Option D
  4. Quick Check:

    res.download needs file path [OK]
Hint: Always provide file path to res.download [OK]
Common Mistakes:
  • Calling res.download without arguments
  • Thinking res.download needs callback first
  • Assuming no error if arguments missing
5. You want to send a file report.pdf for download but also handle errors if the file is missing. Which code correctly does this?
hard
A. res.download('report.pdf', (err) => { if (err) throw err; });
B. res.download('report.pdf').catch(() => res.status(404).send('File not found'));
C. res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); });
D. res.download('report.pdf', 'report.pdf', err => { if (err) res.send('Error'); });

Solution

  1. Step 1: Understand error handling in res.download

    res.download accepts an optional callback as the last argument to handle errors.
  2. Step 2: Check each option

    res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); }); uses the callback correctly to send 404 if error occurs. res.download('report.pdf').catch(() => res.status(404).send('File not found')); wrongly uses .catch which is not supported. res.download('report.pdf', 'report.pdf', err => { if (err) res.send('Error'); }); has an extra filename argument which is unnecessary here. res.download('report.pdf', (err) => { if (err) throw err; }); throws error instead of handling gracefully.
  3. Final Answer:

    res.download('report.pdf', err => { if (err) res.status(404).send('File not found'); }); -> Option C
  4. Quick Check:

    Use callback to handle errors in res.download [OK]
Hint: Use callback function to catch errors in res.download [OK]
Common Mistakes:
  • Using promises or .catch with res.download
  • Not handling errors at all
  • Passing wrong number of arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard