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

Popular posts from this blog

Slip 7 Create a node js file named main.js for event-driven application. There should be a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Slip 1(a) Write an Angular 13 application addition of two numbers using ng-init, ng-model & ng-bind. And also demonstrate ng-show, ng-disabled, ng-click directives on button component.