How to read the content of a directory

var express = require("express");
var router = express.Router();
const {
fromPath
} = require("pdf2pic");
const pdf = require("pdf-page-counter");
const fs = require("fs");
var app = express();
app.get("/getfiles", (req, res, next) => {
foldername = req.query.filename;
const testFolder = `./public/uploads/${foldername}/`;
const tree = dirTree(testFolder);
res.send({
message: tree
});
});
app.get("/pdfToPng", (req, res, next) => {
const {
filename
} = req.query;
rimraf(`./public/uploads/${filename}`, function() {
console.log("remove folder if exists ");
fs.mkdir(`./public/uploads/${filename}`, function(err) {
if (err) {
console.log(err);
} else {
console.log("New directory successfully created.");
const options = {
quality: 100,
saveFilename: filename,
savePath: `./public/uploads/${filename}`,
format: "png",
width: 904,
height: 513,
page: 100,
};
const storeAsImage = fromPath(`./public/uploads/${filename}`, options);
let dataBuffer = fs.readFileSync(`./public/uploads/${filename}`);
pdf(dataBuffer).then(function(data) {
for (var pageToConvertAsImage = 1; pageToConvertAsImage <= data.numpages; pageToConvertAsImage++) {
storeAsImage(pageToConvertAsImage).then((resolve) => {
return resolve;
});
}
});
const testFolder = `./public/uploads/${foldername}/`;
const tree = dirTree(testFolder);
res.send({
message: tree
});
}
});
});
});

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingHub

CodingHub

Coding Hub — Learn & Build Your Career