fork of hey-api/openapi-ts because I need some additional things
1import { dirname, resolve } from 'node:path';
2import { fileURLToPath } from 'node:url';
3
4import {
5 AngularNodeAppEngine,
6 createNodeRequestHandler,
7 isMainModule,
8 writeResponseToNodeResponse,
9} from '@angular/ssr/node';
10import express from 'express';
11
12const serverDistFolder = dirname(fileURLToPath(import.meta.url));
13const browserDistFolder = resolve(serverDistFolder, '../browser');
14
15const app = express();
16const angularApp = new AngularNodeAppEngine();
17
18/**
19 * Example Express Rest API endpoints can be defined here.
20 * Uncomment and define endpoints as necessary.
21 *
22 * Example:
23 * ```ts
24 * app.get('/api/**', (req, res) => {
25 * // Handle API request
26 * });
27 * ```
28 */
29
30/**
31 * Serve static files from /browser
32 */
33app.use(
34 express.static(browserDistFolder, {
35 index: false,
36 maxAge: '1y',
37 redirect: false,
38 }),
39);
40
41/**
42 * Handle all other requests by rendering the Angular application.
43 */
44app.use('/**', (req, res, next) => {
45 angularApp
46 .handle(req)
47 .then((response) => (response ? writeResponseToNodeResponse(response, res) : next()))
48 .catch(next);
49});
50
51/**
52 * Start the server if this module is the main entry point.
53 * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
54 */
55if (isMainModule(import.meta.url)) {
56 const port = process.env['PORT'] || 4000;
57 app.listen(port, () => {
58 console.log(`Node Express server listening on http://localhost:${port}`);
59 });
60}
61
62/**
63 * Request handler used by the Angular CLI (for dev-server and during build) or Firebase Cloud Functions.
64 */
65export const reqHandler = createNodeRequestHandler(app);