|
1 | | -import { APP_BASE_HREF } from '@angular/common'; |
2 | | -import { CommonEngine } from '@angular/ssr/node'; |
3 | | -import express from 'express'; |
4 | | -import { fileURLToPath } from 'node:url'; |
5 | | -import { dirname, join, resolve } from 'node:path'; |
6 | | -import bootstrap from './src/main.server'; |
| 1 | +import { |
| 2 | + AngularNodeAppEngine, |
| 3 | + createNodeRequestHandler, |
| 4 | + isMainModule, |
| 5 | + writeResponseToNodeResponse, |
| 6 | + } from '@angular/ssr/node'; |
| 7 | + import express from 'express'; |
| 8 | + import { dirname, resolve } from 'node:path'; |
| 9 | + import { fileURLToPath } from 'node:url'; |
7 | 10 |
|
8 | | -// The Express app is exported so that it can be used by serverless Functions. |
9 | | -export function app(): express.Express { |
10 | | - const server = express(); |
11 | 11 | const serverDistFolder = dirname(fileURLToPath(import.meta.url)); |
12 | 12 | const browserDistFolder = resolve(serverDistFolder, '../browser'); |
13 | | - const indexHtml = join(serverDistFolder, 'index.server.html'); |
14 | 13 |
|
15 | | - const commonEngine = new CommonEngine(); |
| 14 | + const app = express(); |
| 15 | + const angularApp = new AngularNodeAppEngine(); |
16 | 16 |
|
17 | | - server.set('view engine', 'html'); |
18 | | - server.set('views', browserDistFolder); |
| 17 | + /** |
| 18 | + * Example Express Rest API endpoints can be defined here. |
| 19 | + * Uncomment and define endpoints as necessary. |
| 20 | + * |
| 21 | + * Example: |
| 22 | + * ```ts |
| 23 | + * app.get('/api/**', (req, res) => { |
| 24 | + * // Handle API request |
| 25 | + * }); |
| 26 | + * ``` |
| 27 | + */ |
19 | 28 |
|
20 | | - // Example Express Rest API endpoints |
21 | | - // server.get('/api/**', (req, res) => { }); |
22 | | - // Serve static files from /browser |
23 | | - server.get('*.*', express.static(browserDistFolder, { |
24 | | - maxAge: '1y' |
25 | | - })); |
| 29 | + /** |
| 30 | + * Serve static files from /browser |
| 31 | + */ |
| 32 | + app.use( |
| 33 | + express.static(browserDistFolder, { |
| 34 | + maxAge: '1y', |
| 35 | + index: false, |
| 36 | + redirect: false, |
| 37 | + }), |
| 38 | + ); |
26 | 39 |
|
27 | | - // All regular routes use the Angular engine |
28 | | - server.get('*', (req, res, next) => { |
29 | | - const { protocol, originalUrl, baseUrl, headers } = req; |
30 | | - |
31 | | - commonEngine |
32 | | - .render({ |
33 | | - bootstrap, |
34 | | - documentFilePath: indexHtml, |
35 | | - url: `${protocol}://${headers.host}${originalUrl}`, |
36 | | - publicPath: browserDistFolder, |
37 | | - providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }], |
38 | | - }) |
39 | | - .then((html) => res.send(html)) |
40 | | - .catch((err) => next(err)); |
| 40 | + /** |
| 41 | + * Handle all other requests by rendering the Angular application. |
| 42 | + */ |
| 43 | + app.use('/**', (req, res, next) => { |
| 44 | + angularApp |
| 45 | + .handle(req) |
| 46 | + .then((response) => |
| 47 | + response ? writeResponseToNodeResponse(response, res) : next(), |
| 48 | + ) |
| 49 | + .catch(next); |
41 | 50 | }); |
42 | 51 |
|
43 | | - return server; |
44 | | -} |
45 | | - |
46 | | -function run(): void { |
47 | | - const port = process.env['PORT'] || 4000; |
48 | | - |
49 | | - // Start up the Node server |
50 | | - const server = app(); |
51 | | - server.listen(port, () => { |
52 | | - console.log(`Node Express server listening on http://localhost:${port}`); |
53 | | - }); |
54 | | -} |
| 52 | + /** |
| 53 | + * Start the server if this module is the main entry point. |
| 54 | + * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000. |
| 55 | + */ |
| 56 | + if (isMainModule(import.meta.url)) { |
| 57 | + const port = process.env['PORT'] || 4000; |
| 58 | + app.listen(port, () => { |
| 59 | + console.log(`Node Express server listening on http://localhost:${port}`); |
| 60 | + }); |
| 61 | + } |
55 | 62 |
|
56 | | -run(); |
| 63 | + /** |
| 64 | + * The request handler used by the Angular CLI (dev-server and during build). |
| 65 | + */ |
| 66 | + export const reqHandler = createNodeRequestHandler(app); |
0 commit comments