When the browser accesses a static resource, sometimes the preview screen is opened, and sometimes the file save window is displayed for downloading. So is this default behavior controllable? The answer is yes.
Content-Disposition
Content-disposition is an HTTP response header field.
The values that can be set are:
Inline: Previews a resource
Attachment: Download resource. The file name can be followed by attachment. The download window displays the file name by default.
Server instance
const http = require('http');
const fs = require('fs');
const path = require('path');
const app = http.createServer();
app.on('request'.function (req, res) {
console.log(process.cwd());
fs.readFile(path.resolve(path.dirname(process.argv[1]),'./index.html'), function (err, data) {
res.setHeader('Content-Disposition'.'attachment; filename="index.html"')
res.writeHead(200);
res.end(data.toString().toString())
})
})
console.log('App is open on Port 9000... ');
app.listen('9000')
Copy the code
reference
Developer.mozilla.org/en-US/docs/…