-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (75 loc) · 2.26 KB
/
index.js
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
87
import express from "express"
import mongoose from "mongoose"
import keys from "./config/keys.js"
import { Category, Event, Patient } from "./models/Schemas.js"
const app = express()
app.use(express.json())
mongoose.connect(keys.mongoURI)
// Patient search
app.get("/api/search/patients", async (req, res) => {
let result = await Patient.aggregate([
{
$search: {
autocomplete: {
path: "fullName",
query: `${req.query.q}`
}
}
}, {
$limit: 10
}, {
$project: {
_id: 1,
fullName: 1,
dateOfBirth: 1
}
}
])
res.send(result)
})
// Get 10 latest updated patients
app.get("/api/patients/popular", async (req, res) => {
const patients = await Patient.find().sort({ updatedAt: -1 }).limit(10).select("_id fullName dateOfBirth")
res.send(patients)
})
// Get patient main info
app.get("/api/patients/:patientId", async (req, res) => {
const patientId = req.params.patientId
const patient = await Patient.findById(patientId)
res.send(patient)
})
// Get all events of a given patient
app.get("/api/patients/:patientId/events", async (req, res) => {
const patientId = req.params.patientId
const limit = req.query.n
const events = await Event.find({ patient: patientId })
.limit(limit)
.populate({
path: "category",
model: "Category"
})
res.send(events)
})
// Get all events under a category of a given patient
app.get("/api/patients/:patientId/categories/:categoryId", async (req, res) => {
const patientId = req.params.patientId
const categoryId = req.params.categoryId
const events = await Event.find({ patient: patientId, category: categoryId })
.populate({
path: "category",
model: "Category"
})
res.send(events)
})
import path from "path"
import { fileURLToPath } from "url"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
app.use(express.static(path.join(__dirname, "public")))
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"))
})
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log("Backend server is running on port 5000.")
})