Using Node to access FTP in another server - node.js

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.

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!

Provide a download link to the user from a direct link without saving file in server

I want to create an Express App to provide download links for user.
I put a direct link to a route, and the user is directed to download directly
Like this :
https://myserver.com/?url=(some direct link to file)&type=(file extension)&dlheader=(file type)/(file extension)&title=(file name to download)
Note: the direct link is from other server
You can take url query paramaters and send files via node.js like this. But you didn't provide information about where is your files on server.
app.get('/', async function(req, res) {
// take paramaters from url
const url = req.query.url;
const type= req.query.type;
const dlheader= req.query.dlheader;
const title= req.query.title;
//then send file
res.sendFile(`/your/file/path/${title}.${type}`)
});

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.

Express.js - How to serve files from an arbitrary directory?

I'm trying to serve an arbitrary directory (from a node.js app), but I keep getting Cannot GET /
var express = require('express');
var app = express();
app.use("C:/test/", express.static('C:/test/'))
I want it to display a webpage that shows a list of files (of the specified directory) and the download link. So that when I visit the URL from local devices I would see something like:
C:/test/file1.png - download link
C:/test/file2.mp4 - download link
Is that possible?
It looks like the easiest way to send a single file is to use download method:
app.get('/uniquePathHere', function (req, res) {
res.download('C:/test/1.png', '1.png');
})
On a local device open the IP of the server (IPv4 address assigned by the router to the computer), for example:
192.168.0.105:3000/uniquePathHere
And it will start downloading automatically
If you want to display all the files of the directory, you can use the solution suggested in the comments:
app.use('/ftp', express.static('C:/test/'), serveIndex('C:/test/', {'icons': true}))
And then visit IP of the server, for example:
192.168.0.105:3000/ftp
How to get server IP
var ip = require("ip")
console.log(ip.address())

How to create a proxy download in Nodejs

I want to create a nodejs server which is acting a proxy to download files i.e. user clicks
on the a download button, call get from nodejs server, nodejs server fetches link from a different
remote server and starts the download (in terabytes). This download is then forwarded to the user.
The terabyte file should not be stored on the nodejs server and then sent.
Here is my attempt:
function (request, response) {
// anything related to the remote server having the file
var options= {
path: "./bigData",
hostname:"www.hugeFiles.net"
}
// get the file from the remote server hugefiles and push to user's response
https.get(options, function(downFile)) {
downFile.pipe(response)
}
}
Before I was using res.download(file, function(err)) {} but file has to be downloaded completely from the remote server
You're very close, you're sending the right http body but with the wrong http headers.
Here's a minimal working example:
const express = require('express');
const http = require('http');
const app1 = express();
app1.get('/', function (req, res) {
res.download('server.js');
});
app1.listen(8000);
const app2 = express();
app2.get('/', function (req, res) {
http.get({ path: '/', hostname: 'localhost', port: 8000}, function (resp) {
res.setHeader('content-disposition', resp.headers['content-disposition']);
res.setHeader('Content-type', resp.headers['content-type']);
resp.pipe(res);
});
});
app2.listen(9000);
Though I would say you should take a look at modules like https://github.com/nodejitsu/node-http-proxy which take care of the header etc . . . for you.
There is no way for your server to provide a file to the client without the server downloading it first.
What you may want to do instead is provide the client with a download link to the huge file. To make it seem automatic, you can create html which starts a download from the content provider automatically and serve that to the client.
In other words, in the scenario you are describing, the server is acting as a middleman between your client and the content provider. Unless the server needs to process the data or the client isn't allowed to retrieve the data themselves, it makes more sense to cut out the middleman.

Resources