Skip to content

Commit 42b29a6

Browse files
committed
Add support to return 404 status code #1
- configure project structure - configure lazy loaded modules
1 parent 6567d46 commit 42b29a6

35 files changed

+253
-33
lines changed

project/application/e2e/src/app.e2e-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ describe('workspace-project App', () => {
99

1010
it('should display welcome message', () => {
1111
page.navigateTo();
12-
expect(page.getParagraphText()).toEqual('Welcome to angular ssr app!');
12+
expect(page.getParagraphText()).toEqual('Home page!');
1313
});
1414
});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>{{ title }}</h1>
1+
<router-outlet></router-outlet>

project/application/src/app/app.component.spec.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { TestBed, async } from '@angular/core/testing';
2+
import { RouterTestingModule } from '@angular/router/testing';
3+
24
import { AppComponent } from './app.component';
5+
36
describe('AppComponent', () => {
47
beforeEach(
58
async(() => {
69
TestBed.configureTestingModule({
10+
imports: [RouterTestingModule],
711
declarations: [AppComponent],
812
}).compileComponents();
913
})
@@ -24,13 +28,4 @@ describe('AppComponent', () => {
2428
expect(app.title).toEqual('Welcome to angular ssr app!');
2529
})
2630
);
27-
it(
28-
'should render title in a h1 tag',
29-
async(() => {
30-
const fixture = TestBed.createComponent(AppComponent);
31-
fixture.detectChanges();
32-
const compiled = fixture.debugElement.nativeElement;
33-
expect(compiled.querySelector('h1').textContent).toContain('Welcome to angular ssr app!');
34-
})
35-
);
3631
});

project/application/src/app/app.config.ts

-3
This file was deleted.
+6-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import { NgModule } from '@angular/core';
2-
import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';
3-
import { PLATFORM_ID, APP_ID, Inject } from '@angular/core';
4-
import { isPlatformBrowser } from '@angular/common';
2+
import { RouterModule } from '@angular/router';
53

6-
import { AppComponent } from '@angular-universal/app.component';
4+
import { AppComponent } from './app.component';
5+
import { CoreModule } from './modules/core/core.module';
6+
import { SharedModule } from './modules/shared/shared.module';
77

88
@NgModule({
9-
imports: [BrowserModule.withServerTransition({ appId: 'angular-universal' }), BrowserTransferStateModule],
9+
imports: [RouterModule, CoreModule, SharedModule],
1010
declarations: [AppComponent],
11-
providers: [],
1211
bootstrap: [AppComponent],
1312
})
14-
export class AppModule {
15-
constructor(@Inject(PLATFORM_ID) private platformId: Object, @Inject(APP_ID) private appId: string) {
16-
const platform = isPlatformBrowser(platformId) ? 'in the browser' : 'on the server';
17-
18-
console.log(`Running ${platform} with appId=${appId}`);
19-
}
20-
}
13+
export class AppModule {}

project/application/src/app/app.server.module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { NgModule } from '@angular/core';
22
import { ServerModule, ServerTransferStateModule } from '@angular/platform-server';
33
import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader';
44

5-
import { AppModule } from '@angular-universal/app.module';
6-
import { AppComponent } from '@angular-universal/app.component';
5+
import { AppModule } from './app.module';
6+
import { AppComponent } from './app.component';
77

88
@NgModule({
99
imports: [AppModule, ServerModule, ModuleMapLoaderModule, ServerTransferStateModule],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { environment } from '../../environments/environment';
2+
3+
export const AppSettingsConfig: any = environment;

project/application/src/app/modules/core/authentication/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { NgModule } from '@angular/core';
2+
import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser';
3+
4+
import { RoutingModule } from '../routing/routing.module';
5+
6+
@NgModule({
7+
imports: [
8+
BrowserModule.withServerTransition({ appId: 'angular-universal' }),
9+
BrowserTransferStateModule,
10+
RoutingModule,
11+
],
12+
})
13+
export class CoreModule {}

project/application/src/app/modules/core/guards/.gitkeep

Whitespace-only changes.

project/application/src/app/modules/core/http/.gitkeep

Whitespace-only changes.

project/application/src/app/modules/core/interceptors/.gitkeep

Whitespace-only changes.

project/application/src/app/modules/core/mocks/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { HttpResponseStatusService } from './http-response-status.service';
4+
5+
describe('HttpResponseStatusService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [HttpResponseStatusService],
9+
});
10+
});
11+
12+
it(
13+
'should be created',
14+
inject([HttpResponseStatusService], (service: HttpResponseStatusService) => {
15+
expect(service).toBeTruthy();
16+
})
17+
);
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Inject, Injectable, Optional } from '@angular/core';
2+
import { RESPONSE } from '@nguniversal/express-engine/tokens';
3+
4+
import { CoreModule } from '../../core.module';
5+
6+
@Injectable({
7+
providedIn: CoreModule,
8+
})
9+
export class HttpResponseStatusService {
10+
constructor(
11+
@Optional()
12+
@Inject(RESPONSE)
13+
private res: any
14+
) {}
15+
16+
public setStatus(code: number, message: string): void {
17+
if (this.res) {
18+
this.res.statusCode = code;
19+
this.res.statusMessage = message;
20+
}
21+
}
22+
}

project/application/src/app/modules/home/components/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
import { HomeComponent } from './home.component';
5+
6+
export const routes: Routes = [
7+
{
8+
path: '',
9+
component: HomeComponent,
10+
},
11+
];
12+
13+
@NgModule({
14+
imports: [RouterModule.forChild(routes)],
15+
})
16+
export class HomeRoutingModule {}

project/application/src/app/modules/home/home.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{{ title }}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { TestBed, async } from '@angular/core/testing';
2+
3+
import { HomeComponent } from './home.component';
4+
5+
describe('HomeComponent', () => {
6+
beforeEach(
7+
async(() => {
8+
TestBed.configureTestingModule({ declarations: [HomeComponent] }).compileComponents();
9+
})
10+
);
11+
it(
12+
'should create the app',
13+
async(() => {
14+
const fixture = TestBed.createComponent(HomeComponent);
15+
const app = fixture.debugElement.componentInstance;
16+
expect(app).toBeTruthy();
17+
})
18+
);
19+
it(
20+
`should have as title 'Home page!'`,
21+
async(() => {
22+
const fixture = TestBed.createComponent(HomeComponent);
23+
const app = fixture.debugElement.componentInstance;
24+
expect(app.title).toEqual('Home page!');
25+
})
26+
);
27+
it(
28+
'should render title in a h1 tag',
29+
async(() => {
30+
const fixture = TestBed.createComponent(HomeComponent);
31+
fixture.detectChanges();
32+
const compiled = fixture.debugElement.nativeElement;
33+
expect(compiled.querySelector('h1').textContent).toContain('Home page!');
34+
})
35+
);
36+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-home',
5+
templateUrl: './home.component.html',
6+
styleUrls: ['./home.component.css'],
7+
})
8+
export class HomeComponent {
9+
title = 'Home page!';
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule } from '@angular/core';
2+
3+
import { HomeRoutingModule } from './home-routing.module';
4+
import { HomeComponent } from './home.component';
5+
6+
@NgModule({
7+
imports: [HomeRoutingModule],
8+
declarations: [HomeComponent],
9+
})
10+
export class HomeModule {}

project/application/src/app/modules/not-found/components/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
import { NotFoundComponent } from './not-found.component';
5+
6+
export const routes: Routes = [
7+
{
8+
path: '',
9+
component: NotFoundComponent,
10+
},
11+
];
12+
13+
@NgModule({
14+
imports: [RouterModule.forChild(routes)],
15+
})
16+
export class NotFoundRoutingModule {}

project/application/src/app/modules/not-found/not-found.component.css

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{{ title }}</h1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { TestBed, async } from '@angular/core/testing';
2+
import { RouterTestingModule } from '@angular/router/testing';
3+
4+
import { NotFoundComponent } from './not-found.component';
5+
import { CoreModule } from '../core/core.module';
6+
7+
describe('NotFoundComponent', () => {
8+
beforeEach(
9+
async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [NotFoundComponent],
12+
imports: [CoreModule, RouterTestingModule],
13+
}).compileComponents();
14+
})
15+
);
16+
it(
17+
'should create the app',
18+
async(() => {
19+
const fixture = TestBed.createComponent(NotFoundComponent);
20+
const app = fixture.debugElement.componentInstance;
21+
expect(app).toBeTruthy();
22+
})
23+
);
24+
it(
25+
`should have as title 'Not found page!'`,
26+
async(() => {
27+
const fixture = TestBed.createComponent(NotFoundComponent);
28+
const app = fixture.debugElement.componentInstance;
29+
expect(app.title).toEqual('Not found page!');
30+
})
31+
);
32+
it(
33+
'should render title in a h1 tag',
34+
async(() => {
35+
const fixture = TestBed.createComponent(NotFoundComponent);
36+
fixture.detectChanges();
37+
const compiled = fixture.debugElement.nativeElement;
38+
expect(compiled.querySelector('h1').textContent).toContain('Not found page!');
39+
})
40+
);
41+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
import { HttpResponseStatusService } from '../core/services/http-response-status/http-response-status.service';
4+
5+
@Component({
6+
selector: 'app-not-found',
7+
templateUrl: './not-found.component.html',
8+
styleUrls: ['./not-found.component.css'],
9+
})
10+
export class NotFoundComponent implements OnInit {
11+
title = 'Not found page!';
12+
13+
constructor(private httpResponseStatusService: HttpResponseStatusService) {}
14+
15+
ngOnInit() {
16+
this.httpResponseStatusService.setStatus(404, 'Not Found');
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule } from '@angular/core';
2+
3+
import { NotFoundComponent } from './not-found.component';
4+
import { NotFoundRoutingModule } from './not-found-routing.module';
5+
6+
@NgModule({
7+
imports: [NotFoundRoutingModule],
8+
declarations: [NotFoundComponent],
9+
})
10+
export class NotFoundModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
export const routes: Routes = [
5+
{
6+
path: '',
7+
loadChildren: '../home/home.module#HomeModule',
8+
},
9+
{
10+
path: '**',
11+
loadChildren: '../not-found/not-found.module#NotFoundModule',
12+
},
13+
];
14+
15+
@NgModule({
16+
imports: [RouterModule.forRoot(routes, { initialNavigation: 'enabled' })],
17+
exports: [RouterModule],
18+
})
19+
export class RoutingModule {}

project/application/src/app/modules/shared/components/.gitkeep

Whitespace-only changes.

project/application/src/app/modules/shared/directives/.gitkeep

Whitespace-only changes.

project/application/src/app/modules/shared/pipes/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { NgModule } from '@angular/core';
2+
3+
@NgModule({})
4+
export class SharedModule {}

project/application/tsconfig.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
"target": "es5",
1111
"typeRoots": ["node_modules/@types"],
1212
"lib": ["es2017", "dom"],
13-
"baseUrl": "src",
14-
"paths": {
15-
"@angular-universal/*": ["app/*"]
16-
}
13+
"baseUrl": "./",
1714
}
1815
}

0 commit comments

Comments
 (0)