How to hide a download path of a file with Nodejs and Express - node.js

I have a situation where I want to hide the source path or download path of a file in my server from displaying on html. However, user is still be able to download it.
Any plugin for this? It's build with Nodejs and Express.

Perhaps you should create an API for it? Then you wont give away your file/folder structure.
router.get('/api/download-file', (req, res) => {
res.sendFile(your_file, {headers: {'Content-Type': 'your_file_type'}})
})

Related

How to create file uploading web service using node js with jwt Authentication?

I want to create file uploading web service with jwt Authentication such that peoples can upload there files to /upload route and I will use cloudinary service in my server to actually store them. User can also get their files using filename with /getfile route. Is there something else I should add in my project?
In order for the express server to be able to take a file as an input I would use the package Express file-upload in order to achieve this. The docs are super simple for this one.
Inside of your server.js file:
// setting temp files location
app.use(fileUpload({
useTempFiles : true,
tempFileDir : '/tmp/'
}));
upload route:
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
res.download('/report-12345.pdf')
});
This file obeject caqn then be used to save the file wherever you desire.
For sending the file back to the user Express also has a feature for this which is documented here
getfile route:
app.get('/getfile', function(req, res) {
// whatever file to be downloaded here
res.download('/report-12345.pdf')
});
Hope that's what you were asking for. Thanks!

How to dynamically create/modify sitemap.xml file in angular 9 project structure at runtime?

How to create dynamic sitemap.xml file in Angular 9 project structure ? I can get data using HTTP get request from node API but using this data how can I update local xml file from angular project at runtime ?
In my case, angular project and node project are on different server(different domain). And It is possible to create xml file in node project but I am not able to access that file as xml file in angular project root .
Can anyone please suggest me how to use node xml in angular project OR is it possible to create/modify dynamic sitemap.xml using api call in Angular 9?
Any help would be greatly appreciated.
I am working on the same subject now and I have achieved something.
I added it in the angular.json file like this
Then I created a component and specified its path as sitemap.xml.
It is possible to perform server-side rendering in Angular, and with that, our job is a little easier. Angular Server Side Rendering
You can add a new routing to your routing.module file and interfere with this request via server.ts
For example, you can access a route like sitemap.xml in this way in server.ts.
server.get('/sitemap.xml', (req, res, next) => { bla,bla }
You can now access the file you want here and change it as you wish.
File operations in server.ts
function writeFile(sitemap: string): void {
const fs = require('fs');
fs.appendFile('../sitemap.xml', sitemap, (err) => {
if (err) {
throw err;
}
console.log('Saved!');
});
}
then, redirect operations
res.redirect('http://localhost:{$PORT}/sitemap.xml');

How to make sure download prompt is triggered using express sendFile?

I have the code below in my express server
app.get('/download', async (req, res)=> {
res.sendFile( `/dl/myfile.mp4`, {headers: {'Content-Type': 'mp4'},root: __dirname})
});
The sent file does not cause a download prompt in any of my tested browsers ( Chrome, Opera ) instead it shows me a player and starts playing the file while I need to it cause a download prompt.
How can I avoid the player and trigger a download prompt using Express?
You shoule use res.download(filePathToServer)
Transfers the file at path as an “attachment”. Typically, browsers will prompt the user for download if you use it.
You can read more about it here res.download - express

Express JS download zip via endpoint

I'm trying to allow a user to be able to download a zip folder from a server via an express JS api call. I've created my folder with: zip -r download.zip folder-to-zip and am struggling to get it to download.
It seems if I make a GET request to some endpoint, it only downloads it to the server rather than the browser, my endpoint is:
router.get('/download', (req, res) => {
res.download('download.zip')
})
I need to attach something to a button that'll download this zip folder.
I haven't tested this, I'll update my answer when I have.
But I think doing something like:
router.get('/download', (req, res) => {
res.setHeader('Content-type','application/zip');
res.sendFile(__dirname + '/download.zip');
})
This is usually how I download files via express.

How can I build a web app that I can send files to for further manipulation?

I want to build a NodeJS server that accepts a .wav file (1Mb) sent to its single endpoint, then changes the file through AudioContext API and then sends back the response with the result?
The server shouldn't store anything, so, no database required.
How can I achieve this? (or, please correct me if don't understand how things work)
I would do this with express: https://expressjs.com/
and as middleware add express-fileuplaod: https://www.npmjs.com/package/express-fileupload
app.post('/upload', function(req, res) {
console.log(req.files.foo); // the uploaded file object
});
instead of the console.log(); you'd make a readable stream / buffer and then use it in the
AudioContext API
here is also a interesting Article explaining to use this:
https://www.russellgood.com/process-uploaded-file-web-audio-api/

Resources