-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
52 lines (43 loc) · 1.77 KB
/
Copy pathserver.js
File metadata and controls
52 lines (43 loc) · 1.77 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
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import helmet from 'helmet';
import rateLimit from 'express-rate-limit';
import compression from 'compression';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// Trust the App Engine proxy
app.set('trust proxy', 1);
// Security: Set secure HTTP headers
app.use(helmet({
contentSecurityPolicy: {
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
"script-src": ["'self'", "'unsafe-inline'", "https://maps.googleapis.com", "https://apis.google.com", "https://www.gstatic.com"],
"img-src": ["'self'", "data:", "https://*.gstatic.com", "https://*.googleapis.com", "https://*.firebaseapp.com", "https://www.gstatic.com"],
"connect-src": ["'self'", "https://*.googleapis.com", "https://*.firebaseio.com", "https://*.cloudfunctions.net", "https://identitytoolkit.googleapis.com", "https://*.firebaseapp.com"]
},
},
}));
// Security: Rate limiting to prevent brute-force
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 1000, // Increased limit for hackathon traffic
standardHeaders: true,
legacyHeaders: false,
});
app.use(limiter);
// Compress responses
app.use(compression());
// Serve static files from the 'dist' directory
app.use(express.static(path.join(__dirname, 'dist')));
// Support client-side routing by serving index.html for all non-static requests
// Express 5 requires named wildcards — '*' is invalid, use '/{*any}'
app.get('/{*any}', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});