Can someone resolve the files uploading problem in mega using nodejs?

This is my server side code.


const express = require('express');
const cors = require('cors');
const multer = require('multer');
const fs = require('fs');
const {Storage} = require('megajs');
const archiver = require('archiver');
require('dotenv').config();

const app = express();
app.use(cors());

// ENVs

const port = process.env.PORT || 5000;
const megaUser = process.env.MEGA_USER;
const megaPassword = process.env.MEGA_PASSWORD;
const megaFolderName = process.env.MEGA_FOLDERNAME;

// App Interface

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.get('/:key', (req, res) => {
  res.send(`Hello ${req.params.key}`);
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

// Mega storage Setup

let megaStorage;

const storage = new Storage({
  email: megaUser,
  password: megaPassword,
  userAgent: 'myApp/1.0'
});

storage.on('ready', async () => {
  console.log("MEGA connected");
  megaStorage = storage.root.children.find(folder => folder.name === megaFolderName); 
  if (!megaStorage) {
    console.error("Upload folder not found");
  } 
  else {
    console.log("Upload folder found:", megaStorage.name);
    megaStorage.children?.map(folders => folders.children?.map(files => console.log(files.name)));
  }
});

storage.on('error', (err) => {
  console.error("MEGA connection error:", err);
});

/* REST Api */

// Upload

const upload = multer({ storage: multer.memoryStorage() });

app.post('/api/upload/:key', upload.array('files'), async (req, res) => {
  try {
    if (!megaStorage) {
      return res.status(500).send('MEGA storage not initialized');
    }

    const key = req.params.key;

    let folder = megaStorage.children.find(folder => folder.name === key);
    if (!folder) {
      folder = await megaStorage.mkdir({ name: key });
    }
    for (const file of req.files) {
      const result = await folder.upload({ 
        name: file.originalname,
        size: file.size,
        data: file.buffer 
      }).complete;
      console.log(result);
    }
    res.status(200).send('Files uploaded successfully');
  } 
  catch (err) {
    console.error('Error uploading files:', err);
    res.status(500).send('Error uploading files');
  }
});



This is my endpoint for uploading the files. This endpoint created a new folder in the name of key received and upload the received files to the folder using the megajs.

const upload = multer({ storage: multer.memoryStorage() });

app.post('/api/upload/:key', upload.array('files'), async (req, res) => {
  try {
    if (!megaStorage) {
      return res.status(500).send('MEGA storage not initialized');
    }

    const key = req.params.key;

    let folder = megaStorage.children.find(folder => folder.name === key);
    if (!folder) {
      folder = await megaStorage.mkdir({ name: key });
    }
    for (const file of req.files) {
      const result = await folder.upload({ 
        name: file.originalname,
        size: file.size,
        data: file.buffer 
      }).complete;
      console.log(result);
    }
    res.status(200).send('Files uploaded successfully');
  } 
  catch (err) {
    console.error('Error uploading files:', err);
    res.status(500).send('Error uploading files');
  }
});

But when I request from the client side the page just loading forever. It’s not showing any errors and a new folder is created successfully in Mega but the received files are not uploaded to it. Can someone point me what is the problem?

for (const file of req.files) {
   const result = await folder.upload({ 
      name: file.originalname,
      size: file.size,
      data: file.buffer 
   }).complete;
   console.log(result);
}

This is the part which uploads the files to mega folder and it’s not working. Can anyone tell me to what to do?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật