-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
31 lines (29 loc) · 963 Bytes
/
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
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const fs = require('fs');
const CLEAN_REGEX = new RegExp('//+', 'g');
const YOUR_ACCESS_TOKEN = /<YOUR_ACCESS_TOKEN>/g;
app.use((req, res, next) => {
const file = `./examples/${req.url}`.replace(CLEAN_REGEX, '/');
if (fs.existsSync(file)) {
if (req.url.endsWith('.html')) {
res.send(fs.readFileSync(file).toString().replace(YOUR_ACCESS_TOKEN, process.env.JAWG_ACCESS_TOKEN));
} else {
const files = fs.readdirSync(file) || [];
res.send(`
<html><head><title>Index of ${file}</title></head>
<body>
<h1>Index of ${file}</h1>
<hr><pre><a href="../">../</a>
${files.map((f) => `<a href="${req.url.endsWith('/') ? './' : req.url.split('/').pop()}/${f}">${f}</a>`).join('\n')}
</pre><hr>
</body></html>
`);
}
}
next();
});
app.listen(port, 'localhost', () => {
console.log(`Example app listening at http://localhost:${port}`);
});