-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.mjs
More file actions
66 lines (58 loc) · 2.2 KB
/
server.mjs
File metadata and controls
66 lines (58 loc) · 2.2 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import express from 'express';
import compression from 'compression';
import path from 'path';
import { fileURLToPath } from 'url';
import { readdir } from 'fs/promises';
import serveStatic from 'serve-static';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const baseDir = process.env.NODE_ENV === 'production' ? './dist' : './src';
const app = express();
// Gzip compression for all files
app.use(compression());
// Serve static files
// Automatically adds etag and last-modifed headers for conditional requests
app.use(
'/',
express.static(path.join(__dirname, baseDir), {
redirect: false,
setHeaders: (res, path) => {
if (serveStatic.mime.lookup(path) === 'text/html') {
// 2 minute browser cache, 1 minute CDN cache, safe to be cached on CDNs as well, will keep old html file when revalidating or upon a server error
res.set('Cache-Control', 'public, max-age=120, s-maxage=60, stale-while-revalidate=86400, stale-if-error=86400');
} else {
// Cache static assets for 1 year on the browser, 7 days on CDNs and keep old file when revalidating or upon server error
res.set('Cache-Control', 'public, max-age=31536000, s-maxage=604800, stale-while-revalidate=86400, stale-if-error=86400');
}
},
})
);
// Get all files recursively
async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(
dirents.map((dirent) => {
const res = path.resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
})
);
return Array.prototype.concat(...files);
}
// Api route to get all files from baseDir recursively
app.get('/api/files', async (req, res) => {
try {
const files = await getFiles(path.join(__dirname, baseDir));
res.json({
success: true,
files: files.map((file) => {
file = file.replace(path.join(__dirname, baseDir), ''); // Leave relative path
file = file.replace('index.html', ''); // Fix service worker bug
return file.replace(/\\/g, '/'); // Turn '\' to '/'
}),
});
} catch (err) {
res.json({ success: false, error: err });
}
});
// Start server on port 4000
app.listen(4000, console.log('Server running on http://localhost:4000/'));