Set the HTTP status code to 404 when a page is not found

The browser was getting 200.

Fixes https://github.com/DSpace/dspace-angular/issues/91
This commit is contained in:
Àlex Magaz Graça
2017-08-31 12:35:00 +02:00
parent b108b0ada5
commit 015b439a39
2 changed files with 34 additions and 3 deletions

View File

@@ -1,12 +1,17 @@
import { Component } from '@angular/core';
import { ServerResponseService } from '../shared/server-response.service';
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'ds-pagenotfound',
styleUrls: ['./pagenotfound.component.scss'],
templateUrl: './pagenotfound.component.html'
templateUrl: './pagenotfound.component.html',
providers: [ServerResponseService],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class PageNotFoundComponent {
data: any = {};
constructor(responseService: ServerResponseService) {
responseService.setNotFound();
}
}

View File

@@ -0,0 +1,26 @@
import { RESPONSE } from '@nguniversal/express-engine/tokens';
import { Inject, Injectable, Optional } from '@angular/core';
import { Response } from 'express';
@Injectable()
export class ServerResponseService {
private response: Response;
constructor(@Optional() @Inject(RESPONSE) response: any) {
this.response = response;
}
setStatus(code: number, message?: string): this {
if (this.response) {
this.response.statusCode = code;
if (message) {
this.response.statusMessage = message;
}
}
return this;
}
setNotFound(message = 'Not found'): this {
return this.setStatus(404, message)
}
}