If you have multiple routes like below:
"/get/myfile1", "/get/myfile2", "/get/myfile
Why don't you make a generic one. which can handle all request and it will solve your link(download_name) problem too. You can do it as below
router.get('/get/:fileName',function(req,res){
res.sendFile('/file_path/'+req.params.fileName)
});
Edit After Comment (EDIT 1):
Sorry, i didn't get your point. I am assuming that if you are developing the backend api, you should have the control of choosing the url too, right ?
Giving an example:
when server side is this:
router.get('/get/:fileName',function(req,res){
res.sendFile('/file_path/'+req.params.fileName)
});
Based on your posted code and implementation. The files which need to get downloaded are finite in number and known files.
assuming there are 2 files : "file1.dat" and "file2.dat"
you can call the api as below right ?
- http://yourapi.com/get/file1.dat
- http://yourapi.com/get/file2.dat
am i missing something ?
EDIT 2:
If that is the case, i think this would solve your problem, instead of using sendFile use res.attachment:
app.get('/get/myfile',function(req,res){
res.attachment('/file.txt');
res.end('Downloaded', 'UTF-8')
});