-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.static.mjs
More file actions
63 lines (51 loc) · 1.71 KB
/
Copy pathserver.static.mjs
File metadata and controls
63 lines (51 loc) · 1.71 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
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join, resolve } from 'node:path';
import compression from 'compression';
import helmet from 'helmet';
const __dirname = dirname(fileURLToPath(import.meta.url));
const BROWSER_DIST = resolve(__dirname, 'dist/personal-website/browser');
const app = express();
// Trust proxy for production environments (Cloudflare, Nginx, etc.)
app.set('trust proxy', true);
// 1. Enable Gzip compression
app.use(compression());
// 2. Security headers via Helmet
app.use(helmet({
contentSecurityPolicy: false,
frameguard: { action: 'deny' }
}));
// 3. Security: Prevent content sniffing
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
next();
});
// Serve static assets with aggressive caching
app.use(express.static(BROWSER_DIST, {
maxAge: '1y',
index: false,
setHeaders: (res, path) => {
if (path.endsWith('.html')) {
res.setHeader('Cache-Control', 'public, max-age=0, must-revalidate');
}
}
}));
// Serve localized index files using Regex for maximum compatibility
const serveIndex = (lang) => (req, res) => {
res.sendFile(join(BROWSER_DIST, lang, 'index.html'));
};
app.get(/^\/de/, serveIndex('de'));
app.get(/^\/en/, serveIndex('en'));
// Root redirect based on language preference
app.get('/', (req, res) => {
const lang = req.acceptsLanguages('de', 'en') || 'de';
res.redirect(`/${lang}`);
});
// Fallback for any other routes (default to DE index)
app.get(/.*/, (req, res) => {
res.status(404).sendFile(join(BROWSER_DIST, 'de/index.html'));
});
const port = process.env['PORT'] || 4200;
app.listen(port, () => {
console.log(`Static server listening on http://localhost:${port}`);
});