0
0
Expressframework~5 mins

res.download for file downloads in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does res.download() do in Express?

res.download() sends a file to the client and prompts them to download it. It sets the right headers so the browser knows it's a file to save.

Click to reveal answer
beginner
What are the main parameters of res.download()?

The main parameters are: path (file path), filename (optional new name for the file), and callback (optional function called after download or on error).

Click to reveal answer
intermediate
How does res.download() differ from res.sendFile()?

res.download() forces the browser to download the file, while res.sendFile() just displays or opens the file in the browser if possible.

Click to reveal answer
intermediate
What happens if the file path in res.download() is incorrect or file is missing?

The callback function receives an error, and Express can handle it (like sending a 404 response). Without a callback, Express sends a 404 response.

Click to reveal answer
beginner
Why is it important to use absolute paths with res.download()?

Absolute paths avoid confusion about where the file is located, preventing errors and security issues by ensuring the correct file is sent.

Click to reveal answer
What does res.download() do in Express?
ASends a file and prompts the user to download it
BSends JSON data to the client
CRenders a view template
DRedirects the client to another URL
Which parameter is NOT part of res.download()?
AFile path
BHTTP status code
CCallback function
DFilename for download
If you want the browser to open a file instead of downloading, which method should you use?
Ares.download()
Bres.redirect()
Cres.sendFile()
Dres.json()
What should you do to handle errors when using res.download()?
ASet a timeout for the download
BIgnore errors, Express handles them automatically
CUse try-catch around <code>res.download()</code>
DUse a callback function to catch errors
Why prefer absolute paths with res.download()?
ATo avoid file path confusion and security issues
BTo improve download speed
CTo change the file name automatically
DTo compress the file before sending
Explain how to use res.download() to send a file for download in Express.
Think about what parameters you pass and what happens in the browser.
You got /4 concepts.
    Describe the difference between res.download() and res.sendFile() in Express.
    Consider how the browser treats the file in each case.
    You got /4 concepts.