I am new with node.js. Here is my problem: I upload a file from html
<form method="POST" action="/upload" enctype="multipart/form-data"
<div>
<input type="file" name="myFile" id="fileUpload" multiple />
</div>
<div>
<input type="submit" name="btn_upload" value="Upload" />
</div>
</form>
then I upload the file from directory with multer and parse with the npm pdfParser from pdf to json, the files are regularly copied in the main directory and the code goes back to home page.
try {
app.post("/upload", (req, res) => {
upload(req, res, (err) => {
if (err) {
console.log(err);
} else {
if (req.file == undefined) {
res.redirect("index.html");
} else {
fileOriginale = req.file.originalname;
const pdfParser = new PDFParser();
pdfParser.on("pdfParser_dataError", (errData) =>
console.error(errData.parserError)
);
pdfParser.on("pdfParser_dataReady", (pdfData) => {
const test = fileOriginale.substring(0, fileOriginale.length - 4);
const id = __dirname + "/" + test + ".json";
console.log("filetofetch in pdfParser", id);
fs.writeFile(
id,
JSON.stringify(pdfData),
(err) => console.error(err)
);
});
pdfParser.loadPDF(__dirname + "/" + fileOriginale.toString());
res.redirect("index.html");
res.end();
return;
}
}
});
});
} catch (err) {
console.log(err);
return;
}
Here the issue: How can I parse the .json file loaded in the main directory and send it to the client ?