0

My Express, typescript application is running on a server. I need to upload files to a remote server's private directory from the application server. I have already configured the remote server's nginx configuration, so I can read the file within the remote server(targeted directory), from the application server.

I have used Multer to upload files. When I tried to upload a file in the remote server's directory, it is saying the directory is not found. I tried to use the directory creation command 'if no directory found' . But it do not work as well. I am not sure how can upload files to a remote server.

const storage = multer.memoryStorage();

const upload = multer({ storage });

appRoutes.patch('/update', verifyAdmin, tryCatch(upload.single(
  'apn_prod_cert_file_name'
)),
  tryCatch((req, res, next) => {
    
    const apnProdCertFileName = req.file;

    tryCatchPromise(appService.Update(req.body, apnProdCertFileName), res, next);
  }));

UpdateApps(data: any, apnProdCertFileName: any) {
        return new Promise(async (resolve, reject) => {

           

            if (apnProdCertFileName) {

                const filename = apnProdCertFileName.fileName;
                const newpath = process.env.appAssetUrl + "/apn_certs/" + filename;
                // process.env.appAssetUrl is webserver address
                if (!fs.existsSync(process.env.appAssetUrl + "/apn_certs")) {
                    fs.mkdirSync(process.env.appAssetUrl + "/apn_certs", { recursive: true});
                    fs.writeFileSync(newpath, apnProdCertFileName.buffer, { flag: 'a+' });
                    resolve(true);
                }
                else {
                    fs.writeFileSync(newpath, apnProdCertFileName.buffer, { flag: 'a+' });
                    resolve(true);
                }

            } else{
                reject(false);
           }

      
        });

    }


Error: ENOENT: no such file or directory, mkdir 'domainaddress/apn_certs'

Rajib
  • 392
  • 4
  • 16

0 Answers0