-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
67 lines (54 loc) · 1.98 KB
/
Copy pathapp.js
File metadata and controls
67 lines (54 loc) · 1.98 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
67
// app.js
// Express application factory: wires middleware, routes, and the template
// renderer. Exported without calling listen() so tests can bind to an
// ephemeral port, and server.js can own process-level concerns (signals,
// logging, PORT resolution).
'use strict';
const path = require('path');
const express = require('express');
const helmet = require('helmet');
const compression = require('compression');
const apiRouter = require('./routes/api');
const { renderIndex } = require('./lib/render');
const ROOT = __dirname;
function createApp() {
const app = express();
// Security headers. CSP is intentionally off: the existing front-end
// relies on jQuery plugins setting element.style directly, and locking
// CSP down properly is a separate hardening task that deserves its own
// pass so we don't regress the UI.
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
crossOriginResourcePolicy: { policy: 'cross-origin' }
})
);
// Gzip/Brotli on responses above ~1 KB. Big wins on main.css and jquery.min.js.
app.use(compression());
// Long-lived cache headers on static assets cut bandwidth and backend load.
app.use(
'/public',
express.static(path.join(ROOT, 'public'), { maxAge: '7d', etag: true })
);
// Content API — the single seam the front-end uses to talk to the backend.
app.use('/api', apiRouter);
// SPA fallback: render the composed HTML for any non-API, non-static path.
app.get('/*', async (req, res, next) => {
try {
const html = await renderIndex();
res.set('Content-Type', 'text/html; charset=utf-8');
res.set('Cache-Control', 'public, max-age=60');
res.send(html);
} catch (err) {
next(err);
}
});
// Centralized error handler — keep this LAST.
app.use((err, req, res, _next) => {
console.error('[app]', err);
res.status(500).json({ error: 'Internal Server Error' });
});
return app;
}
module.exports = { createApp };