I want to view a pdf sent from my backend (nodeJS)
my route
router.route("/getPdf").post(getPdf);
I need to use "POST" req as my pdfs are saved using user id's which I need to find the pdf
my controller
exports.getPdf = async (req, res) => {
const id = req.body.id;
var pdf = fs.readFileSync(
path.join(__dirname, "..", "/submits", id + ".pdf")
);
res.contentType("application/pdf")
res.send(pdf);
};
react component
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import axios from "axios";
const PDFPreview = () => {
const { id } = useParams();
useEffect(() => {
try {
var res = axios.post("http://localhost:3000/api/pdf/getPdf", { id });
console.log(res)
} catch (error) {
console.log(error);
}
}, []);
return ("Hi");
};
export default PDFPreview;
