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