2023-04-18 10:27:19 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2023-04-18 10:27:19 +00:00
|
|
|
*/
|
|
|
|
|
/* tslint:disable:no-console */
|
|
|
|
|
import {APP_BASE_HREF} from '@angular/common';
|
|
|
|
|
import {renderModule} from '@angular/platform-server';
|
2023-11-27 09:41:02 +00:00
|
|
|
import express from 'express';
|
2024-09-10 21:04:09 +00:00
|
|
|
import AppServerModule from './src/main.server';
|
2023-11-27 09:41:02 +00:00
|
|
|
import {fileURLToPath} from 'node:url';
|
|
|
|
|
import {dirname, join, resolve} from 'node:path';
|
|
|
|
|
import {readFileSync} from 'node:fs';
|
2023-06-06 10:32:39 +00:00
|
|
|
import './prerender';
|
2023-04-18 10:27:19 +00:00
|
|
|
|
|
|
|
|
const app = express();
|
2023-11-27 09:41:02 +00:00
|
|
|
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
const browserDistFolder = resolve(serverDistFolder, '../browser');
|
2024-04-22 14:53:22 +00:00
|
|
|
const indexHtml = readFileSync(join(browserDistFolder, 'index.csr.html'), 'utf-8');
|
2023-04-18 10:27:19 +00:00
|
|
|
|
|
|
|
|
// Serve static files from /browser
|
2025-09-18 09:46:02 +00:00
|
|
|
app.use(
|
2023-11-27 09:41:02 +00:00
|
|
|
express.static(browserDistFolder, {
|
2023-04-18 10:27:19 +00:00
|
|
|
maxAge: '1y',
|
2023-11-27 09:41:02 +00:00
|
|
|
}),
|
2023-04-18 10:27:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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
|
2025-09-18 09:46:02 +00:00
|
|
|
app.use((req, res) => {
|
2023-11-27 09:41:02 +00:00
|
|
|
const {protocol, originalUrl, baseUrl, headers} = req;
|
2023-10-23 12:58:03 +00:00
|
|
|
|
2023-04-18 10:27:19 +00:00
|
|
|
renderModule(AppServerModule, {
|
|
|
|
|
document: indexHtml,
|
2023-10-23 12:58:03 +00:00
|
|
|
url: `${protocol}://${headers.host}${originalUrl}`,
|
|
|
|
|
extraProviders: [{provide: APP_BASE_HREF, useValue: baseUrl}],
|
2023-04-18 10:27:19 +00:00
|
|
|
}).then((response: string) => {
|
|
|
|
|
res.send(response);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.listen(4206, () => {
|
|
|
|
|
console.log('Server listening on port 4206!');
|
|
|
|
|
});
|