好的,抱歉我误读了你的问题 :)
首先,Apache不再需要为您的应用程序提供服务。这是因为node.js将为您提供服务。
因此,如果您的服务器上不需要apache服务,出于安全考虑和避免应用程序之间的端口冲突,您应该禁用它。
服务器渲染需要修改您的angular应用程序。让我们开始吧。
1)进入您的angular项目并安装以下软件包:
npm i -S @angular/platform-server @angular/animations express
2) 打开
src/app/app.module.ts 文件,并修改下面的注释行,将其替换为您的应用程序名称。
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'your-angular-app-name' }),
FormsModule,
HttpModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
3) 现在,我们将在您的应用程序中设置服务器。为此,请创建文件app/src/app.server.module.ts并添加以下代码。
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [
ServerModule,
AppModule
],
bootstrap: [AppComponent]
})
export class AppServerModule { }
4) 创建文件src/server.ts并添加以下代码。这是Node服务器。
import 'reflect-metadata';
import 'zone.js/dist/zone-node';
import { platformServer, renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory';
import * as express from 'express';
import { readFileSync } from 'fs';
import { join } from 'path';
const PORT = 4000;
enableProdMode();
const app = express();
let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString();
app.engine('html', (_, options, callback) => {
const opts = { document: template, url: options.req.url };
renderModuleFactory(AppServerModuleNgFactory, opts)
.then(html => callback(null, html));
});
app.set('view engine', 'html');
app.set('views', 'src');
app.get('*.*', express.static(join(__dirname, '..', 'dist')));
app.get('*', (req, res) => {
res.render('index', { req });
});
app.listen(PORT, () => {
console.log(`Listening on http://localhost:${PORT}...`);
});
5) 打开 src/tsconfig.app.json 并将文件 server.ts 添加到 exclude json 变量中。
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": []
},
"exclude": [
"server.ts", # Just here !
"test.ts",
"**/*.spec.ts"
]
}
6) 打开位于项目根目录下的tsconfig.json文件,并添加以下angularCompilerOptions选项:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
},
"angularCompilerOptions": {
"genDir": "./dist/ngfactory",
"entryModule": "./src/app/app.module#AppModule"
}
}
7) 现在,修改你的package.json文件,让你的编程变得更加轻松。
{
"scripts": {
"prestart": "ng build --prod && ngc",
"start": "ts-node src/server.ts",
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
}
8) 现在你可以使用以下方式测试你的应用程序:
npm start
希望这能对您有所帮助。
注意:
如果您的项目已经存在,那么您可能会遇到一些模块问题,例如 Angular Material,因为 node.js 不知道 window 对象、document 对象。
因此,您需要:
Cdly。