-1

I trying to upload a zip file and extracting it using unzipper. During unzipping process server restarts multiple time automatically because I am using nodemon. Something is causing file changes I don't know where and that when nodemon getting triggered. Sometimes it sends back response sometime it would not. But without nodemon it works fine.

This is my code:

const fs = require("fs");
const unzipper = require("unzipper");
//models
const Card = require("../models/card");
const H5P = require("../models/contentModels/h5p");

const catchAsync = require("../utils/catchAsync");
const APIerror = require("../utils/APIError");

exports.uploadH5P = catchAsync(async (req, res, next) => {
  const { id, level } = req.body;
  const name = req.file.filename.split(".")[0];
  const folderName = name.split(" ").join("-");
  const cDate = Date.now();

  const filePath = __basedir + `/public/assets/h5p-temp/${req.file.filename}`;

  //checking if the card exists or not
  const check = await Card.findOne({ _id: id });
  if (!check) {
    fs.unlinkSync(filePath);
    return next(
      new APIerror(`No card exists with the given card id :${id}`, 404)
    );
  }

  const unzippedPath =
    __basedir + `/public/assets/h5p/${level}/${folderName}-${cDate}`;
  //reading file and extracting it.
  const stream = fs.ReadStream(filePath).pipe(
    unzipper.Extract({
      path: unzippedPath,
    })
  );

  stream.on("close", () => {
    fs.unlinkSync(filePath);
  });

  const publicPath = `/assets/h5p/${level}/${folderName}-${cDate}`;
  const h5pBody = {
    name: name,
    level: level,
    path: publicPath,
    cardId: id,
  };

  const h5pDoc = await H5P.create(h5pBody);

  res.status(201).json({
    Success: true,
    message: "H5P file is uploaded successfully.",
    h5pDoc,
  });
});

attaching the screenshot screenshot

I have changed the code multiple times like making this process and awaiting it and I even used adm-zip and got the same error.

Addition: I have another post route that for uploading a json file and reading it using fs.readFileSync and the issue happening in there too. server restarts and no response.

For file upload I am using multer.

Here is my multer code

const multer = require("multer");
const APIError = require("../utils/APIError");

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    if (file.mimetype === "application/json") {
      cb(null, `${__basedir}/public/assets/card-content`);
    } else if (file.mimetype === "application/octet-stream") {
      cb(null, `${__basedir}/public/assets/h5p-temp`);
    } else {
      cb(new APIError("Invalid file type.", 400));
    }
  },

  filename: (req, file, cb) => {
    if (file.mimetype === "application/json") {
      cb(null, file.originalname);
    } else if (file.mimetype === "application/octet-stream") {
      const name = file.originalname.split(".")[0];
      const ext = ".zip";
      cb(null, name + ext);
    }
  },
});

module.exports = multer({ storage: storage });
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    The storage is inside the project, and nodemon restarts whenever a file inside your project changes. Either ignore the storage or use a storage outside the project. – jabaa Aug 17 '23 at 09:25
  • I did not know that nodemon watches every file even storage folder. I followed your instructions and the issue is resolved. Thanks alot. – Muhammad Younis Aug 17 '23 at 10:19
  • How could nodemon distinguish project resources from storage files? – jabaa Aug 17 '23 at 11:36

0 Answers0