How to catch and show routing error header resonse, as i am 200 instead of 404 when redirecting to not found page. I want 404 header response when redirecting to not found page.
Loading
How to catch and show routing error header resonse, as i am 200 instead of 404 when redirecting to not found page. I want 404 header response when redirecting to not found page.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Kabir SinghPosted Aug 7, 2021, 10:20 AM
Kabir SinghPosted Aug 4, 2021, 8:13 PM
Pranam BhatPosted Aug 4, 2021, 1:10 PM
Sachin SinghPosted Aug 4, 2021, 12:46 PM
Angular is a SPA technology, for single-page applications, there is no meaning for status codes, everything is done via components and a component can't return HTTP status codes. Your navigation simply returns a component so you will always get 200 Ok.
But, I know for SEO purposes, setting a correct HttpStatus code is very important. for this, you can test this
//Modify below lines in server.ts as
app.get('*', (req, res) => {
res.render('index', {
req: req,
res: res,
providers: [
{
provide: REQUEST, useValue: (req)
},
{
provide: RESPONSE, useValue: (res)
}
]
});
});
// in your 404NotFound component
constructor(@Optional() @Inject(RESPONSE) private response: Response,
@Inject(PLATFORM_ID) private platformId: Object) {}
ngOnInit(){
if(isPlatformServer(this.platformId)){
this.response.status(404);
//or this.response.statusCode=404;
}
}