0
0
Expressframework~10 mins

res.download for file downloads 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 send a file named 'report.pdf' for download using Express.

Express
app.get('/download', (req, res) => {
  res.[1]('report.pdf');
});
Drag options to blanks, or click blank then click option'
Adownload
Bredirect
Csend
DsendFile
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendFile instead of res.download
Using res.redirect which is for URL redirection
2fill in blank
medium

Complete the code to specify a custom filename 'user_report.pdf' for the downloaded file.

Express
app.get('/download', (req, res) => {
  res.download('report.pdf', [1]);
});
Drag options to blanks, or click blank then click option'
A'download.pdf'
B'user_report.pdf'
C'file.pdf'
D'report_download.pdf'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original file path instead of a filename
Omitting quotes around the filename
3fill in blank
hard

Fix the error in the code to handle errors during file download.

Express
app.get('/download', (req, res) => {
  res.download('report.pdf', ([1]) => {
    if (err) {
      res.status(500).send('Error downloading file');
    }
  });
});
Drag options to blanks, or click blank then click option'
Aexception
Berror
Ce
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one checked inside the callback
Not including the error parameter at all
4fill in blank
hard

Fill both blanks to send 'report.pdf' for download and handle errors by sending a 404 status.

Express
app.get('/download', (req, res) => {
  res.[1]('report.pdf', ([2]) => {
    if ([2]) {
      res.status(404).send('File not found');
    }
  });
});
Drag options to blanks, or click blank then click option'
Adownload
Berr
CsendFile
Derror
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendFile instead of res.download
Using a wrong parameter name for the error
5fill in blank
hard

Fill all three blanks to send 'report.pdf' for download with a custom filename 'summary.pdf' and handle errors by logging them.

Express
app.get('/download', (req, res) => {
  res.[1]('report.pdf', [2], ([3]) => {
    if ([3]) {
      console.error('Download error:', [3]);
      res.status(500).send('Download failed');
    }
  });
});
Drag options to blanks, or click blank then click option'
Adownload
B'summary.pdf'
Cerr
DsendFile
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the custom filename argument
Using wrong parameter names in the callback
Using res.sendFile instead of res.download