angular/integration/platform-server-zoneless/projects/standalone/server.ts
Alan Agius 56c0ef8eb7 refactor: replace port 4206 usage with 4209 (#61362)
Port `4206` is used by other tests which causes this test to be flakey example https://github.com/angular/angular/actions/runs/15038891642/job/42265813534

PR Close #61362
2025-05-15 10:12:44 -07: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.dev/license
*/
/* tslint:disable:no-console */
import {APP_BASE_HREF} from '@angular/common';
import {renderApplication} from '@angular/platform-server';
import express from 'express';
import bootstrap 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.csr.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;
renderApplication(bootstrap, {
document: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
platformProviders: [{provide: APP_BASE_HREF, useValue: baseUrl}],
}).then((response: string) => {
res.send(response);
});
});
app.listen(4209, () => {
console.log('Server listening on port 4209!');
});