A
A
Alex Art2019-12-29 14:18:01
Angular
Alex Art, 2019-12-29 14:18:01

How to generate 404 code in Angular Universal?

Good day! Can you please tell me how to specify the 404 response code for the node server in angular? The standard documentation only indicates how to specify it for a specific path '/api/*' but I would like the angular component to be able to indicate this response code to the server. Yes, and through Nginx along the way. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deeno, 2020-12-30
@Deeno

First, (if the component is created, if not, create it) in the app-routing.module.ts file

{
    path: '404', component: NotfoundComponent
  },
  {
    path: '**', redirectTo: '/404'
  }

then in 404.ts you need to import RESPONSE from express-engine and further
1:  import { RESPONSE } from '@nguniversal/express-engine/tokens';
2: constructor(@Optional() @Inject(RESPONSE) private response: any) { }
в ngOnInit() прописать
3: this.response.statusCode = 404;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question