-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathserver.cjs
More file actions
45 lines (36 loc) · 1.23 KB
/
server.cjs
File metadata and controls
45 lines (36 loc) · 1.23 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
const express = require('express');
const path = require('path');
const fs = require('fs');
const rateLimit = require('express-rate-limit');
const cors = require('cors');
const app = express();
const port = 8000;
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')));
const basePath = `/chi/${packageJson.version}`;
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100
});
app.use(cors());
app.use(basePath, express.static(path.join(__dirname, 'dist')));
app.use(limiter);
app.get(/(.*)/, (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
app.get(new RegExp(`${basePath}/tests/(.*)`), (req, res) => {
const requestedPath = req.path.replace(`${basePath}/tests`, '');
const baseTestsPath = path.join(__dirname, 'dist', 'tests');
const testPath = path.resolve(baseTestsPath, requestedPath);
if (!testPath.startsWith(baseTestsPath)) {
res.status(403).send('Forbidden');
return;
}
if (fs.existsSync(testPath) && fs.lstatSync(testPath).isFile()) {
res.sendFile(testPath);
} else {
res.sendFile(path.join(baseTestsPath, 'index.html'));
}
});
app.listen(port, () => {
console.log(`[CHI]: Started on http://localhost:${port}${basePath}`);
});