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);
}
}
}
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);
});
})
由于我自己刚刚偶然发现这个问题,我在一篇博客文章中记录了我的过程。如果您需要更多指导,请查看:
https://blog.thecodecampus.de/angular-universal-handle-404-set-status-codes/