in my express server when I click on upload button or download button I want that my server work with file system(fs) without changing web page. how to implement this code in JS?
let express = require('express');
let Fs = require('fs');
let path = require('path');
let URL = require('url');
let app = express();
let router = express.Router();
const port = 2020;
app.use(express.static('Programs'));
app.use('/', router);
router.get('/' , (req, res) => {
res.sendFile(path.join(__dirname+'/views/UiOfServer.html'));
});
router.get('/todo/download' , (req, res) => {
res.send("download page");
});
router.get('/todo/upload' , (req, res) => {
res.send("upload page");
});
app.listen(port, (err, res) => {
if(err){
console.log(`Server Error: ${err}`);
}
else{
console.log(`server started on ${port}`);
}
})
If you don't want the current page in the browser to change or refresh when clicking a button you should use AJAX.
You should use upload and download endpoints as apis and call these apis from your webpage.
You can use libraries such as jquery, axios, fetch, etc to make your job easier
Example in jquery
$.get('/todo/download').done(response => console.log(response))
Do you mean that you don't want the server to read and write continuously? If it's true, try it
fs.readFileSync(your_data_url);
it should be at the top (maybe after let router = express.Router();) because it is synchronous, and it only runs once when you load web page