-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
86 lines (72 loc) · 2.73 KB
/
Copy pathserver.ts
File metadata and controls
86 lines (72 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import "dotenv/config";
import express from "express";
import { createServer as createViteServer } from "vite";
import multer from "multer";
import * as pdfParseModule from "pdf-parse";
const pdfParse = (pdfParseModule as any).default || pdfParseModule;
import path from "path";
const upload = multer({ storage: multer.memoryStorage() });
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
// API Routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok" });
});
// Upload PDF and extract text chunks
app.post("/api/upload-pdf", upload.single("file"), async (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: "No file uploaded" });
}
const data = await pdfParse(req.file.buffer);
const text = data.text;
// Better chunking logic that preserves paragraphs
// First, normalize multiple newlines to double newlines to retain structural boundaries.
// Then chunk by paragraphs, roughly 2000-3000 characters per chunk.
const normalizedText = text.replace(/\r\n/g, '\n').replace(/\n{3,}/g, '\n\n');
const paragraphs = normalizedText.split(/\n\n/);
const chunks = [];
let currentChunk = "";
const MAX_CHUNK_LENGTH = 3000;
for (const p of paragraphs) {
// Clean up internal breaks inside the paragraph occasionally caused by PDF artifacts
const cleanParagraph = p.replace(/(?<!\n)\n(?!\n)/g, ' ').trim();
if (!cleanParagraph) continue;
if (currentChunk.length + cleanParagraph.length > MAX_CHUNK_LENGTH && currentChunk.length > 0) {
chunks.push(currentChunk);
currentChunk = cleanParagraph;
} else {
currentChunk += (currentChunk ? "\n\n" : "") + cleanParagraph;
}
}
if (currentChunk) {
chunks.push(currentChunk);
}
res.json({ chunks, numPages: data.numpages, info: data.info });
} catch (error) {
console.error("Error parsing PDF:", error);
res.status(500).json({ error: "Failed to parse PDF" });
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();