1
1
// #docplaster
2
2
3
- import { APP_BASE_HREF } from '@angular/common' ;
4
- import { CommonEngine } from '@angular/ssr' ;
3
+ import { APP_BASE_HREF } from '@angular/common' ;
4
+ import { CommonEngine } from '@angular/ssr' ;
5
5
import express from 'express' ;
6
- import { fileURLToPath } from 'node:url' ;
7
- import { dirname , join , resolve } from 'node:path' ;
6
+ import { dirname , join , resolve } from 'node:path' ;
7
+ import { fileURLToPath } from 'node:url' ;
8
+
8
9
import bootstrap from './src/main.server' ;
9
10
10
11
// The Express app is exported so that it can be used by serverless Functions.
@@ -13,42 +14,34 @@ export function app(): express.Express {
13
14
const serverDistFolder = dirname ( fileURLToPath ( import . meta. url ) ) ;
14
15
const browserDistFolder = resolve ( serverDistFolder , '../browser' ) ;
15
16
const indexHtml = join ( serverDistFolder , 'index.server.html' ) ;
16
- // #docregion CommonEngine
17
17
const commonEngine = new CommonEngine ( ) ;
18
- // #enddocregion CommonEngine
19
18
20
19
server . set ( 'view engine' , 'html' ) ;
21
20
server . set ( 'views' , browserDistFolder ) ;
22
21
23
- // #docregion data-request
24
22
// TODO: implement data requests securely
25
23
// Serve data from URLS that begin "/api/"
26
24
server . get ( '/api/**' , ( req , res ) => {
27
25
res . status ( 404 ) . send ( 'data requests are not yet supported' ) ;
28
26
} ) ;
29
- // #enddocregion data-request
30
- // #docregion static
31
27
// Serve static files from /browser
32
- server . get ( '*.*' , express . static ( browserDistFolder , {
33
- maxAge : '1y'
34
- } ) ) ;
35
- // #enddocregion static
28
+ server . get ( '*.*' , express . static ( browserDistFolder , { maxAge : '1y' } ) ) ;
36
29
37
30
// #docregion navigation-request
38
31
// All regular routes use the Angular engine
39
32
server . get ( '*' , ( req , res , next ) => {
40
- const { protocol, originalUrl, baseUrl, headers } = req ;
33
+ const { protocol, originalUrl, baseUrl, headers} = req ;
41
34
42
35
commonEngine
43
- . render ( {
44
- bootstrap,
45
- documentFilePath : indexHtml ,
46
- url : `${ protocol } ://${ headers . host } ${ originalUrl } ` ,
47
- publicPath : browserDistFolder ,
48
- providers : [ { provide : APP_BASE_HREF , useValue : req . baseUrl } ] ,
49
- } )
50
- . then ( ( html ) => res . send ( html ) )
51
- . catch ( ( err ) => next ( err ) ) ;
36
+ . render ( {
37
+ bootstrap,
38
+ documentFilePath : indexHtml ,
39
+ url : `${ protocol } ://${ headers . host } ${ originalUrl } ` ,
40
+ publicPath : browserDistFolder ,
41
+ providers : [ { provide : APP_BASE_HREF , useValue : req . baseUrl } ] ,
42
+ } )
43
+ . then ( ( html ) => res . send ( html ) )
44
+ . catch ( ( err ) => next ( err ) ) ;
52
45
} ) ;
53
46
// #enddocregion navigation-request
54
47
0 commit comments