Slip 11: Write node js application that transfer a file as an attachment on web and enables browser to prompt the user to download file using express js.
const express = require('express');
const path = require('path');
const app = express();
const PORT = 3000;
// Route to download file
app.get('/download', (req, res) => {
res.download('a.txt', (err) => {
if (err) {
console.log("Error downloading file:", err);
} else {
console.log("File sent successfully");
}
});
});
// Home
app.get('/', (req, res) => {
res.send('<h2>Click below to download file</h2><a href="/download">Download File</a>');
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Comments
Post a Comment