setup1
This commit is contained in:
@@ -3,6 +3,7 @@ import patientRoutes from './patients';
|
||||
import appointmentRoutes from './appointements'
|
||||
import userRoutes from './users'
|
||||
import staffRoutes from './staffs'
|
||||
import pdfExtractionRoutes from './pdfExtraction';
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -10,5 +11,6 @@ router.use('/patients', patientRoutes);
|
||||
router.use('/appointments', appointmentRoutes);
|
||||
router.use('/users', userRoutes);
|
||||
router.use('/staffs', staffRoutes);
|
||||
router.use('/pdfExtraction/', pdfExtractionRoutes);
|
||||
|
||||
export default router;
|
||||
23
apps/Backend/src/routes/pdfExtraction.ts
Normal file
23
apps/Backend/src/routes/pdfExtraction.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
const router = Router();
|
||||
import multer from "multer";
|
||||
import forwardToPythonService from "../services/pythonClient";
|
||||
|
||||
const upload = multer({ storage: multer.memoryStorage() });
|
||||
|
||||
router.post("/extract", upload.single("pdf"), async (req: Request, res: Response): Promise<any>=> {
|
||||
if (!req.file) {
|
||||
return res.status(400).json({ error: "No PDF file uploaded." });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await forwardToPythonService(req.file);
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Extraction failed" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
30
apps/Backend/src/services/pythonClient.ts
Normal file
30
apps/Backend/src/services/pythonClient.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import axios from "axios";
|
||||
import FormData from "form-data";
|
||||
import { Express } from "express";
|
||||
|
||||
export interface ExtractedData {
|
||||
name?: string;
|
||||
memberId?: string;
|
||||
dob?: string;
|
||||
[key: string]: any; // In case your extraction returns additional dynamic fields
|
||||
}
|
||||
|
||||
export default async function forwardToPythonService(
|
||||
file: Express.Multer.File
|
||||
): Promise<ExtractedData> {
|
||||
const form = new FormData();
|
||||
form.append("pdf", file.buffer, {
|
||||
filename: file.originalname,
|
||||
contentType: file.mimetype,
|
||||
});
|
||||
|
||||
const response = await axios.post<ExtractedData>(
|
||||
"http://localhost:5001/extract",
|
||||
form,
|
||||
{
|
||||
headers: form.getHeaders(),
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
Reference in New Issue
Block a user