要创建一个Angular 9应用程序,其中包含通用(Universal)和express-engine的解决方案,可以按照以下步骤进行操作:
npm install -g @angular/cli
ng new my-app
cd my-app
npm install express @nguniversal/express-engine
ng generate universal my-universal-app
cd src/app/my-universal-app
npm install @nguniversal/module-map-ngfactory-loader
server.ts
:import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { enableProdMode } from '@angular/core';
import { renderModuleFactory } from '@angular/platform-server';
import { AppServerModuleNgFactory, LAZY_MODULE_MAP } from './main.universal';
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';
enableProdMode();
const app = express();
const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist/my-app/browser');
const template = readFileSync(join(DIST_FOLDER, 'index.html')).toString();
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./main.universal');
app.engine('html', (_, options, callback) => {
const opts = { document: template, url: options.req.url };
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html))
.catch(err => callback(err));
});
app.set('view engine', 'html');
app.set('views', DIST_FOLDER);
app.get('*.*', express.static(DIST_FOLDER, { maxAge: '1y' }));
app.get('*', (req, res) => {
res.render('index', { req });
});
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
src/main.ts
文件,使其适应通用应用程序:import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
export { AppServerModule } from './app/app.server.module'; // 添加此行
if (environment.production) {
enableProdMode();
}
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', bootstrap);
} else {
bootstrap();
}
src/app/app.server.module.ts
文件,使其适应通用应用程序:import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [
AppModule,
ServerModule,
ModuleMapLoaderModule // 添加此行
],
bootstrap: [AppComponent],
})
export class AppServerModule {}
angular.json
文件,以包含通用应用程序的构建配置:{
"projects": {
"my-app": {
"architect": {
"build": {
"configurations": {
"server": {
"fileReplacements": [
{
"replace": "src/main.ts",
"with": "src/main.server.ts"
}
],
"outputPath": "dist/my-app/server",
"main": "src/main.server.ts",
"tsConfig": "src/tsconfig.server.json"
}
}
}
}
}
}
}
src/main.server.ts
文件,以启动通用应用程序:export { AppServerModule } from './app/app.server.module'; // 添加此行
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
export { renderModule, renderModuleFactory