|
| 1 | +import { match } from 'node:assert'; |
| 2 | +import { readFile, writeMultipleFiles } from '../../utils/fs'; |
| 3 | +import { noSilentNg, silentNg } from '../../utils/process'; |
| 4 | +import { setupProjectWithSSRAppEngine } from './setup'; |
| 5 | + |
| 6 | +export default async function () { |
| 7 | + // Setup project |
| 8 | + await setupProjectWithSSRAppEngine(); |
| 9 | + |
| 10 | + await writeMultipleFiles({ |
| 11 | + // Add asset |
| 12 | + 'public/media.json': JSON.stringify({ dataFromAssets: true }), |
| 13 | + // Update component to do an HTTP call to asset and API. |
| 14 | + 'src/app/app.component.ts': ` |
| 15 | + import { Component, inject } from '@angular/core'; |
| 16 | + import { JsonPipe } from '@angular/common'; |
| 17 | + import { RouterOutlet } from '@angular/router'; |
| 18 | + import { HttpClient } from '@angular/common/http'; |
| 19 | +
|
| 20 | + @Component({ |
| 21 | + selector: 'app-root', |
| 22 | + standalone: true, |
| 23 | + imports: [JsonPipe, RouterOutlet], |
| 24 | + template: \` |
| 25 | + <p>{{ assetsData | json }}</p> |
| 26 | + <p>{{ apiData | json }}</p> |
| 27 | + <router-outlet></router-outlet> |
| 28 | + \`, |
| 29 | + }) |
| 30 | + export class AppComponent { |
| 31 | + assetsData: any; |
| 32 | + apiData: any; |
| 33 | +
|
| 34 | + constructor() { |
| 35 | + const http = inject(HttpClient); |
| 36 | +
|
| 37 | + http.get('/media.json').toPromise().then((d) => { |
| 38 | + this.assetsData = d; |
| 39 | + }); |
| 40 | +
|
| 41 | + http.get('/api').toPromise().then((d) => { |
| 42 | + this.apiData = d; |
| 43 | + }); |
| 44 | + } |
| 45 | + } |
| 46 | + `, |
| 47 | + // Add http client and route |
| 48 | + 'src/app/app.config.ts': ` |
| 49 | + import { ApplicationConfig } from '@angular/core'; |
| 50 | + import { provideRouter } from '@angular/router'; |
| 51 | +
|
| 52 | + import { HomeComponent } from './home/home.component'; |
| 53 | + import { provideClientHydration } from '@angular/platform-browser'; |
| 54 | + import { provideHttpClient, withFetch } from '@angular/common/http'; |
| 55 | +
|
| 56 | + export const appConfig: ApplicationConfig = { |
| 57 | + providers: [ |
| 58 | + provideRouter([{ |
| 59 | + path: 'home', |
| 60 | + component: HomeComponent, |
| 61 | + }]), |
| 62 | + provideClientHydration(), |
| 63 | + provideHttpClient(withFetch()), |
| 64 | + ], |
| 65 | + }; |
| 66 | + `, |
| 67 | + 'src/app/app.routes.server.ts': ` |
| 68 | + import { RenderMode, ServerRoute } from '@angular/ssr'; |
| 69 | +
|
| 70 | + export const routes: ServerRoute[] = [ |
| 71 | + { |
| 72 | + path: '**', |
| 73 | + renderMode: RenderMode.Prerender, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + `, |
| 77 | + 'server.ts': ` |
| 78 | + import { AngularNodeAppEngine, writeResponseToNodeResponse, isMainModule, createNodeRequestHandler } from '@angular/ssr/node'; |
| 79 | + import express from 'express'; |
| 80 | + import { fileURLToPath } from 'node:url'; |
| 81 | + import { dirname, resolve } from 'node:path'; |
| 82 | +
|
| 83 | + export function app(): express.Express { |
| 84 | + const server = express(); |
| 85 | + const serverDistFolder = dirname(fileURLToPath(import.meta.url)); |
| 86 | + const browserDistFolder = resolve(serverDistFolder, '../browser'); |
| 87 | + const angularNodeAppEngine = new AngularNodeAppEngine(); |
| 88 | +
|
| 89 | + server.use('/api', (req, res) => res.json({ dataFromAPI: true })); |
| 90 | +
|
| 91 | + server.get('**', express.static(browserDistFolder, { |
| 92 | + maxAge: '1y', |
| 93 | + index: 'index.html' |
| 94 | + })); |
| 95 | +
|
| 96 | + server.get('**', (req, res, next) => { |
| 97 | + angularNodeAppEngine.render(req) |
| 98 | + .then((response) => response ? writeResponseToNodeResponse(response, res) : next()) |
| 99 | + .catch(next); |
| 100 | + }); |
| 101 | +
|
| 102 | + return server; |
| 103 | + } |
| 104 | +
|
| 105 | + const server = app(); |
| 106 | + if (isMainModule(import.meta.url)) { |
| 107 | + const port = process.env['PORT'] || 4000; |
| 108 | + server.listen(port, () => { |
| 109 | + console.log(\`Node Express server listening on http://localhost:\${port}\`); |
| 110 | + }); |
| 111 | + } |
| 112 | +
|
| 113 | + export default createNodeRequestHandler(server); |
| 114 | + `, |
| 115 | + }); |
| 116 | + |
| 117 | + await silentNg('generate', 'component', 'home'); |
| 118 | + |
| 119 | + // Fix the error |
| 120 | + await noSilentNg('build', '--output-mode=static'); |
| 121 | + |
| 122 | + const contents = await readFile('dist/test-project/browser/home/index.html'); |
| 123 | + match(contents, /<p>{[\S\s]*"dataFromAssets":[\s\S]*true[\S\s]*}<\/p>/); |
| 124 | + match(contents, /<p>{[\S\s]*"dataFromAPI":[\s\S]*true[\S\s]*}<\/p>/); |
| 125 | + match(contents, /home works!/); |
| 126 | +} |
0 commit comments