-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (69 loc) · 2.25 KB
/
server.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
import express from "express";
import fetch from "node-fetch";
import path from "path";
import fs from "fs"; // Import the fs module
import { fileURLToPath } from "url";
// __dirname equivalent in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = 3000;
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, "public")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
app.get("/api/raids/nyc", async (req, res) => {
try {
const response = await fetch("https://nycpokemap.com/raids.php");
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).json({ error: "Failed to fetch raids data" });
}
});
app.get("/api/raids/sgp", async (req, res) => {
try {
const response = await fetch("https://sgpokemap.com/raids.php");
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).json({ error: "Failed to fetch raids data" });
}
});
app.get("/api/raids/sydney", async (req, res) => {
try {
const response = await fetch("https://sydneypogomap.com/raids.php");
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).json({ error: "Failed to fetch raids data" });
}
});
// Serving the pokedex data
app.get("/api/pokedex", (req, res) => {
const pokedexPath = path.join(__dirname, "Data", "pokedexdata.json");
fs.readFile(pokedexPath, "utf8", (err, data) => {
if (err) {
console.error("Error reading the pokedex file:", err);
res.status(500).json({ error: "Unable to read pokedex file" });
} else {
res.json(JSON.parse(data));
}
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});