Express JS download zip via endpoint - node.js

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.

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 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

nodeJS not found on firebase

:)
i have a nodeJS app on firebase, the front of this app is do with ReactJS.
As long as I do not update the web page everything works perfectly, but if I update, nodeJS takes priority over the url and displays an error because no action is defined for the url
I made a sendFile to the index.html for each url like that :
app.get('*', (req, res) => {
res.sendFile('index.html', {root: `${__dirname}/../public`});
});
Locally, it works perfectly, I can update without losing the page where I am, but when I deploy my application on firebase it does not work. during the update it shows me "not found" and that in the console of the browser
console of the brower
I thought that it could not find the file because firebase arranged them differently but I did not find anything concerning the architecture of firebase :/
I don't know if it's completely clear, I don't know how to explain it better ^^ '
thanks for your time :)
I found!, firebase apparently does not allow the code to leave the function folder (donation impossible to do ../public), so I simply put the public folder in the function folder .
Its gives its:
app.get('*', (req, res) => {
res.sendFile('index.html', {root: `${__dirname}/public`});
});

Using Node to access FTP in another server

I need to access FTP in another server (Ubuntu).
My Node.js API receive an image from user, and then needs to upload it to another server using FTP connection. However, if user folder doesn't exist, I need to create folder before sending the image.
How can i do this?
I'm using express-upload to get files:
const express = require('express');
const upload = require('express-fileupload');
const app = express();
app.use(upload());
app.use('/upload', async (req, res, next) => {
console.log(req.files.image);
})
You can use Basic FTP, an FTP client module, and use its ensureDir() method to implement the requirement "if user folder doesn't exists, I need to create folder before sending image".
According to its document:
...we make sure a remote path exists, creating all directories as necessary.
await client.ensureDir("my/remote/directory")
Then, you can send the image using its upload() method.

How to hide a download path of a file with Nodejs and Express

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'}})
})

Resources