-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
47 lines (37 loc) · 1.36 KB
/
server.js
File metadata and controls
47 lines (37 loc) · 1.36 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
const parseUrl = require("parseurl");
const send = require("send");
const path = require("path");
const fs = require("fs");
const express = require("express");
const app = express();
const PORT = 8000;
app.use(express.static("build"));
// if request failed, try to do case insensitive search on the path (i love the barotrauma devs, i love the barotrauma devs, i love the barotrauma devs)
app.use((req, res, next) => {
const url = parseUrl(req);
let fixedPath = "build";
for (const part of url.pathname.split("/")) {
const oldPath = fixedPath;
fixedPath = path.join(oldPath, part);
// this is horribly slow and woefully insecure, but it works good enough for now
try {
fs.statSync(fixedPath);
} catch {
const correctCase = fs.readdirSync(oldPath).find(folderName => folderName.toLowerCase() == part.toLowerCase());
if(correctCase)
fixedPath = path.join(oldPath, correctCase);
else
next();
}
}
let forwardError = false;
const stream = send(req, fixedPath);
stream.on("file", () => {
forwardError = true;
});
stream.on("error", (err) => {
next(forwardError ? err : undefined);
});
stream.pipe(res);
});
app.listen(PORT, () => { console.log("Server started on port "+PORT) });