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

Upvotes: 1

Views: 934

Answers (1)

shaochuancs
shaochuancs

Reputation: 16256

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.

Upvotes: 2

Related Questions