angular/integration/platform-server/projects/ngmodule/server.ts
Alan Agius e8ad51aaed test: refactor platform-server integration tests to use application builder (#53205)
This commit updates the platform-server tests to use the new application builder, with this change we also have to remove the sizechecks since esbuild will do code motion and split the code into multiple chunks example `chunk-QUKLKPSE.js`.

PR Close #53205
2023-11-28 11:07:01 +01:00

55 lines
1.5 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/* tslint:disable:no-console */
import {APP_BASE_HREF} from '@angular/common';
import {renderModule} from '@angular/platform-server';
import express from 'express';
import {AppServerModule} from './src/main.server';
import {fileURLToPath} from 'node:url';
import {dirname, join, resolve} from 'node:path';
import {readFileSync} from 'node:fs';
import './prerender';
const app = express();
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');
const indexHtml = readFileSync(join(browserDistFolder, 'index.html'), 'utf-8');
// Serve static files from /browser
app.get(
'*.*',
express.static(browserDistFolder, {
maxAge: '1y',
}),
);
// Mock API
app.get('/api', (req, res) => {
res.json({data: 'API 1 response'});
});
app.get('/api-2', (req, res) => {
res.json({data: 'API 2 response'});
});
// All regular routes use the Universal engine
app.get('*', (req, res) => {
const {protocol, originalUrl, baseUrl, headers} = req;
renderModule(AppServerModule, {
document: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
extraProviders: [{provide: APP_BASE_HREF, useValue: baseUrl}],
}).then((response: string) => {
res.send(response);
});
});
app.listen(4206, () => {
console.log('Server listening on port 4206!');
});