-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (79 loc) · 2.72 KB
/
server.js
File metadata and controls
88 lines (79 loc) · 2.72 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
87
88
const express = require("express");
const cors = require("cors");
const path = require("path");
const { runDijkstra } = require("./algorithms/dijkstra");
const { runBellmanFord } = require("./algorithms/bellmanFord");
const { runTsinghua } = require("./algorithms/tsinghua");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.get("/api/health", (req, res) => {
res.json({ ok: true });
});
app.post("/api/shortest-path/dijkstra", (req, res) => {
try {
const { nodes, edges, sourceId, targetId } = req.body;
if (!nodes || !edges || sourceId === undefined) {
return res
.status(400)
.json({ error: "nodes, edges, and sourceId are required" });
}
const result = runDijkstra({ nodes, edges, sourceId, targetId });
res.json(result);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/api/shortest-path/bellman-ford", (req, res) => {
try {
const { nodes, edges, sourceId, targetId } = req.body;
if (!nodes || !edges || sourceId === undefined) {
return res
.status(400)
.json({ error: "nodes, edges, and sourceId are required" });
}
const result = runBellmanFord({ nodes, edges, sourceId, targetId });
res.json(result);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/api/shortest-path/tsinghua", (req, res) => {
try {
const { nodes, edges, sourceId, targetId } = req.body;
if (!nodes || !edges || sourceId === undefined) {
return res
.status(400)
.json({ error: "nodes, edges, and sourceId are required" });
}
const result = runTsinghua({ nodes, edges, sourceId, targetId });
res.json(result);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.post("/api/shortest-path/compare", (req, res) => {
try {
const { nodes, edges, sourceId, targetId } = req.body;
if (!nodes || !edges || sourceId === undefined) {
return res
.status(400)
.json({ error: "nodes, edges, and sourceId are required" });
}
const dijkstra = runDijkstra({ nodes, edges, sourceId, targetId });
const bellmanFord = runBellmanFord({ nodes, edges, sourceId, targetId });
const tsinghua = runTsinghua({ nodes, edges, sourceId, targetId });
res.json({ dijkstra, bellmanFord, tsinghua });
} catch (error) {
console.error(error);
res.status(500).json({ error: "Internal server error" });
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});