-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (35 loc) · 1.08 KB
/
server.js
File metadata and controls
40 lines (35 loc) · 1.08 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
const express = require('express');
const path = require('path');
const compression = require('compression'); // gzip
const app = express();
const distFolder = path.join(__dirname, 'dist/portfolio/browser');
// Abilita gzip su tutti i file
app.use(
compression({
level: 9, // massima compressione gzip
threshold: 0, // comprime tutti i file
})
);
// Serve static con cache headers
app.use(
express.static(distFolder, {
setHeaders: (res, filePath) => {
if (filePath.match(/\.(?:js|css|woff2?|avif|webp|png|jpg|svg)$/)) {
// Cache lunga per file hashati
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
} else if (filePath.endsWith('index.html')) {
// Cache breve per HTML
res.setHeader('Cache-Control', 'public, max-age=60, stale-while-revalidate=300');
}
},
})
);
// Fallback SPA per tutte le route
app.use((req, res) => {
res.sendFile(path.join(distFolder, 'index.html'));
});
// Porta locale
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});