I think you're possibly doing something else wrong. Here's a functional example:
const path = require('path');
const express = require('express');
const app = express();
app.get('/download-docx', (req, res) => {
const docPath = path.join(__dirname, 'demo.docx');
res.download(docPath, 'demo.docx', function(err){
if (err) {
// if the file download fails, we throw an error
throw err;
}
});
})
const listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port);
});
It assumes there's a demo.docx
file in the same directory the above file is in. If you'd like a completely functional example you can edit I built one for you on glitch.com. One small catch, glitch doesn't show the demo.docx
file in the file tree, but rest assured it is actually there. You can confirm for yourself using the console option in the Advanced Options menu in the top left.