-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (70 loc) · 2.7 KB
/
Copy pathindex.js
File metadata and controls
82 lines (70 loc) · 2.7 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
/**
* WHAT THIS FILE DOES
*
* The front door. Defines the two HTTP endpoints and starts the server.
*
* POST /ingest -> hands the uploaded PDF to ingest.js
* POST /chat -> hands the question to query.js
*
* This file deliberately contains no RAG logic of its own. It validates the
* request, calls one function, and turns the result (or the error) into JSON.
*
* See docs/04-code-walkthrough.md for a line-by-line explanation.
*
* @author Raqibul Hasan Moon <rhmoon21@gmail.com>
* @created 2026-08-12
*/
require("dotenv").config();
const express = require("express");
const multer = require("multer");
const { initDb } = require("./db");
const { ingestDocument } = require("./ingest");
const { queryDocuments } = require("./query");
const app = express();
// memoryStorage() keeps the uploaded file in a buffer instead of writing it to disk. We parse it and store the chunks immediately, so there's nothing to save.
const upload = multer({ storage: multer.memoryStorage() });
app.use(express.json());
app.post("/ingest", upload.single("file"), async (req, res) => {
if (!req.file) {
return res.status(400).json({ error: "No file uploaded" });
}
// mimetype comes from the client's multipart headers, so this is a
// convenience check that produces a clean 400 for honest mistakes — not a
// security control. A forged Content-Type still reaches the parser, which
// rejects non-PDF bytes on its own and surfaces as a 500 below.
if (!req.file.mimetype.includes("pdf")) {
return res.status(400).json({ error: "Only PDF files are supported" });
}
try {
const count = await ingestDocument(req.file.buffer, req.file.originalname);
res.json({
message: `Ingested ${count} chunks from "${req.file.originalname}"`,
});
} catch (err) {
console.error(err);
res.status(500).json({ error: "Ingestion failed", detail: err.message });
}
});
app.post("/chat", async (req, res) => {
const { question } = req.body;
if (!question || typeof question !== "string") {
return res.status(400).json({ error: "question is required" });
}
try {
const result = await queryDocuments(question);
res.json(result);
} catch (err) {
console.error(err);
res.status(500).json({ error: "Query failed", detail: err.message });
}
});
const PORT = process.env.PORT || 3000;
// Listen only after the schema is ready, so the server never accepts a request
// it cannot serve. If Postgres is unreachable this rejects and the process
// exits non-zero — the correct behaviour: a supervisor should restart it rather
// than leave a half-working server accepting traffic.
initDb().then(() => {
app.listen(PORT, () => {
console.log(`RAG chatbot running on port ${PORT}`);
});
});