mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
Prior to this change in some cases errors tht happen during routing were not being surfaced. This is due to the fact that the router has floating promises, and the platform was being destroyed prior to these being settled. PR Close #50587
52 lines
1.3 KiB
TypeScript
52 lines
1.3 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 'zone.js/node';
|
|
import {APP_BASE_HREF} from '@angular/common';
|
|
import {renderModule} from '@angular/platform-server';
|
|
import * as express from 'express';
|
|
import {AppServerModule} from './src/main.server';
|
|
import {join} from 'path';
|
|
import {readFileSync} from 'fs';
|
|
import './prerender';
|
|
|
|
const app = express();
|
|
const distFolder = join(process.cwd(), 'dist/ngmodule/browser');
|
|
const indexHtml = readFileSync(join(distFolder, 'index.html'), 'utf-8');
|
|
|
|
// Serve static files from /browser
|
|
app.get(
|
|
'*.*',
|
|
express.static(distFolder, {
|
|
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) => {
|
|
renderModule(AppServerModule, {
|
|
document: indexHtml,
|
|
url: req.url,
|
|
extraProviders: [{provide: APP_BASE_HREF, useValue: req.baseUrl}],
|
|
}).then((response: string) => {
|
|
res.send(response);
|
|
});
|
|
});
|
|
|
|
app.listen(4206, () => {
|
|
console.log('Server listening on port 4206!');
|
|
});
|