добавить это к компоненту NotFoundComponent
import {Component, Inject, OnInit, Optional, PLATFORM_ID} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {RESPONSE} from '@nguniversal/express-engine/tokens';
import {Response} from 'express';
@Component({
selector: 'app-not-found',
templateUrl: './not-found.component.html',
styleUrls: ['./not-found.component.scss']
})
export class NotFoundComponent implements OnInit {
constructor(@Inject(PLATFORM_ID) private platformId: Object,
@Optional() @Inject(RESPONSE) private response: Response) {
}
ngOnInit() {
if (!isPlatformBrowser(this.platformId)) {
this.response.status(404);
}
}
}
изменить файл server.js к этому
import {REQUEST, RESPONSE} from '@nguniversal/express-engine/tokens';
import {ValueProvider} from '@angular/core';
app.engine('html', (_, options, callback) => {
renderModuleFactory(AppServerModuleNgFactory, {
// Our index.html
document: template,
url: options.req.url,
extraProviders: [
// make req and response accessible when angular app runs on server
<ValueProvider>{
provide: REQUEST,
useValue: options.req
},
<ValueProvider>{
provide: RESPONSE,
useValue: options.req.res,
},
]
}).then(html => {
callback(null, html);
});
});