This repository was archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (35 loc) · 1.38 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
const express = require('express'),
app = express(),
path = require('path'),
fs = require('fs'),
config = require('./config.json');
app.get('/index.css', (req, res) => {
res.sendFile(path.join(__dirname, "index.css"));
});
app.get('/visualize.js', (req, res) => {
res.sendFile(path.join(__dirname, "visualize.js"));
});
app.use("/themes", express.static(path.join(__dirname, "themes/")));
app.use("/algos", express.static(path.join(__dirname, "algos/")));
app.use("/sorting_algos", express.static(path.join(__dirname, "sorting_algos/")));
app.get('/:theme', (req, res) => {
switch (req.params.theme) {
case 'dark':
{
fs.readFile(path.join(__dirname, "index.html"), (err, data) => {
if (err) throw err;
let script = "changeTheme(\"dark\");";
res.send(data.toString().substring(0, data.toString().lastIndexOf('</script>')) + script +
data.toString().substring(data.toString().lastIndexOf('</script>')));
});
}
break;
default:
res.sendFile(path.join(__dirname, "index.html"));
break;
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
app.listen(config.port, () => console.log(` [WEBSERVER]: Listening on :${config.port} ... `));