forked from RikudouSage/AiHordeFrontpage
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.ts
More file actions
59 lines (52 loc) · 1.47 KB
/
Copy pathserver.ts
File metadata and controls
59 lines (52 loc) · 1.47 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
// server.ts
import {
AngularNodeAppEngine,
createNodeRequestHandler,
isMainModule,
writeResponseToNodeResponse,
} from '@angular/ssr/node';
import express from 'express';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const app = express();
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const allowedHosts = (
process.env['NG_ALLOWED_HOSTS'] ?? 'localhost,127.0.0.1,::1'
)
.split(',')
.map((host) => host.trim())
.filter((host) => host.length > 0);
const angularApp = new AngularNodeAppEngine({ allowedHosts });
app.use(
express.static(browserDistFolder, {
index: false,
maxAge: '1y',
}),
);
app.get('/healthz', (_req, res) => {
res.status(200).json({ status: 'ok' });
});
app.use('/{*splat}', (req, res, next) => {
void angularApp
.handle(req)
.then((response) => {
if (response) {
void writeResponseToNodeResponse(response, res);
} else {
next();
}
})
.catch(next);
});
/**
* The request handler used by the Angular CLI (dev-server and during build).
*/
export const reqHandler = createNodeRequestHandler(app);
if (isMainModule(import.meta.url)) {
const port = Number(process.env['PORT'] ?? 4000);
app.listen(port, '0.0.0.0', () => {
console.log(`Allowed SSR hosts: ${allowedHosts.join(', ')}`);
console.log(`Node Express server listening on http://0.0.0.0:${port}`);
});
}