-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (46 loc) · 1.57 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
const express = require("express");
const fs = require("fs");
const path = require("path");
const app = express();
const port = 3000;
app.use(express.static(path.join(__dirname, "public")));
app.get("/api/fortnite-maps", (req, res) => {
const mapsPath = path.join(__dirname, "public/assets/fortnite-maps");
const categories = {};
const subDirs = fs.readdirSync(mapsPath, { withFileTypes: true })
.filter(entry => entry.isDirectory())
.map(entry => entry.name)
.sort((a, b) => b.localeCompare(a)); // reversen damit die neusten maps zuerst kommen
for (const subDir of subDirs) {
const categoryPath = path.join(mapsPath, subDir);
const images = [];
const files = fs.readdirSync(categoryPath);
const sortedFiles = files
.filter(file => file.match(/\.(jpg|webp|png)$/i))
.sort((a, b) => {
const regex = /(\d+(\.\d+)?)/;
const matchA = a.match(regex);
const matchB = b.match(regex);
if (matchA && matchB) {
const numA = parseFloat(matchA[1]);
const numB = parseFloat(matchB[1]);
return numB - numA;
}
return 0;
});
for (const file of sortedFiles) {
const imagePath = path.join("/assets/fortnite-maps", subDir, file);
const imageTitle = file.replace(/\.(jpg|webp|png)$/i, "");
images.push({
path: imagePath,
link: imagePath,
title: imageTitle,
});
}
categories[subDir] = images;
}
res.json(categories);
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});