The res.attachment() function sets the Content-Disposition HTTP response header to attachment, indicating that the response should be treated as a downloadable file. When a filename is provided, Express also sets the Content-Type based on the file extension and adds the filename to the Content-Disposition header.

Syntax:Â
res.attachment( [filename] )Parameter: The filename parameter is optional and specifies the filename to include in the Content-Disposition header.
Return Value: Returns the response object (res).
Installation of the Express module
You can install this package by using this command.Â
npm install expressAfter installing the express module, you can check your express version in the command prompt using the command.Â
npm version expressAfter that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.Â
node index.jsProject Structure:

Example 1: Setting Content-Disposition
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.attachment('Hello.txt');
console.log(res.get('Content-Disposition'));
console.log(res.get('Content-Type'));
res.send('Response attachment header set successfully.');
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
Steps to run the program:Â
Run the index.js file using the below command:Â
node index.jsOutput:
Console Output: Â
Server listening on PORT 3000Browser Output:Â
Open your browser and go to http://localhost:3000/, now you can see the following output on your browser:Â

Console Output:

Example 2: Using res.attachment() Without a Filename
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.attachment();
console.log(res.get('Content-Disposition'));
res.send('Attachment header set without a filename.');
});
app.listen(PORT, () => {
console.log(`Server listening on PORT ${PORT}`);
});
Steps to run the program:Â
Run the index.js file using the below command:Â
node index.jsOutput:
Console Output: Â
Server listening on PORT 3000Browser Output:Â
Open your browser and go to http://localhost:3000/, now you can see the following output on your browser:Â

Console Output:

Working of res.attachment()
- An Express application receives a client request.
- The route handler gets access to the response object through res.
- res.attachment() sets the Content-Disposition response header to attachment.
- When a filename is provided, Express adds it to the filename parameter.
- Express also determines the Content-Type from the filename extension.
- The response can then be sent using methods such as res.send() or res.sendFile().
Use Cases of res.attachment()
- Setting a response to be treated as a downloadable attachment.
- Setting a filename for a file response.
- Specifying the appropriate content type based on a file extension.
- Preparing response headers before sending a file.
- Controlling how browsers handle file responses.