From 42b29a6a6412f5b5c1fee40f428436ccd0359271 Mon Sep 17 00:00:00 2001 From: Alexander G Date: Mon, 13 Aug 2018 23:55:55 +0300 Subject: [PATCH] Add support to return 404 status code #1 - configure project structure - configure lazy loaded modules --- project/application/e2e/src/app.e2e-spec.ts | 2 +- .../application/src/app/app.component.html | 2 +- .../application/src/app/app.component.spec.ts | 13 ++---- project/application/src/app/app.config.ts | 3 -- project/application/src/app/app.module.ts | 19 +++------ .../application/src/app/app.server.module.ts | 4 +- .../src/app/configs/app-settings.config.ts | 3 ++ .../app/modules/core/authentication/.gitkeep | 0 .../src/app/modules/core/core.module.ts | 13 ++++++ .../src/app/modules/core/guards/.gitkeep | 0 .../src/app/modules/core/http/.gitkeep | 0 .../app/modules/core/interceptors/.gitkeep | 0 .../src/app/modules/core/mocks/.gitkeep | 0 .../http-response-status.service.spec.ts | 18 ++++++++ .../http-response-status.service.ts | 22 ++++++++++ .../src/app/modules/home/components/.gitkeep | 0 .../app/modules/home/home-routing.module.ts | 16 ++++++++ .../src/app/modules/home/home.component.css | 0 .../src/app/modules/home/home.component.html | 1 + .../app/modules/home/home.component.spec.ts | 36 ++++++++++++++++ .../src/app/modules/home/home.component.ts | 10 +++++ .../src/app/modules/home/home.module.ts | 10 +++++ .../app/modules/not-found/components/.gitkeep | 0 .../not-found/not-found-routing.module.ts | 16 ++++++++ .../modules/not-found/not-found.component.css | 0 .../not-found/not-found.component.html | 1 + .../not-found/not-found.component.spec.ts | 41 +++++++++++++++++++ .../modules/not-found/not-found.component.ts | 18 ++++++++ .../app/modules/not-found/not-found.module.ts | 10 +++++ .../src/app/modules/routing/routing.module.ts | 19 +++++++++ .../app/modules/shared/components/.gitkeep | 0 .../app/modules/shared/directives/.gitkeep | 0 .../src/app/modules/shared/pipes/.gitkeep | 0 .../src/app/modules/shared/shared.module.ts | 4 ++ project/application/tsconfig.json | 5 +-- 35 files changed, 253 insertions(+), 33 deletions(-) delete mode 100644 project/application/src/app/app.config.ts create mode 100644 project/application/src/app/configs/app-settings.config.ts create mode 100644 project/application/src/app/modules/core/authentication/.gitkeep create mode 100644 project/application/src/app/modules/core/core.module.ts create mode 100644 project/application/src/app/modules/core/guards/.gitkeep create mode 100644 project/application/src/app/modules/core/http/.gitkeep create mode 100644 project/application/src/app/modules/core/interceptors/.gitkeep create mode 100644 project/application/src/app/modules/core/mocks/.gitkeep create mode 100644 project/application/src/app/modules/core/services/http-response-status/http-response-status.service.spec.ts create mode 100644 project/application/src/app/modules/core/services/http-response-status/http-response-status.service.ts create mode 100644 project/application/src/app/modules/home/components/.gitkeep create mode 100644 project/application/src/app/modules/home/home-routing.module.ts create mode 100644 project/application/src/app/modules/home/home.component.css create mode 100644 project/application/src/app/modules/home/home.component.html create mode 100644 project/application/src/app/modules/home/home.component.spec.ts create mode 100644 project/application/src/app/modules/home/home.component.ts create mode 100644 project/application/src/app/modules/home/home.module.ts create mode 100644 project/application/src/app/modules/not-found/components/.gitkeep create mode 100644 project/application/src/app/modules/not-found/not-found-routing.module.ts create mode 100644 project/application/src/app/modules/not-found/not-found.component.css create mode 100644 project/application/src/app/modules/not-found/not-found.component.html create mode 100644 project/application/src/app/modules/not-found/not-found.component.spec.ts create mode 100644 project/application/src/app/modules/not-found/not-found.component.ts create mode 100644 project/application/src/app/modules/not-found/not-found.module.ts create mode 100644 project/application/src/app/modules/routing/routing.module.ts create mode 100644 project/application/src/app/modules/shared/components/.gitkeep create mode 100644 project/application/src/app/modules/shared/directives/.gitkeep create mode 100644 project/application/src/app/modules/shared/pipes/.gitkeep create mode 100644 project/application/src/app/modules/shared/shared.module.ts diff --git a/project/application/e2e/src/app.e2e-spec.ts b/project/application/e2e/src/app.e2e-spec.ts index 8001126..dad782b 100644 --- a/project/application/e2e/src/app.e2e-spec.ts +++ b/project/application/e2e/src/app.e2e-spec.ts @@ -9,6 +9,6 @@ describe('workspace-project App', () => { it('should display welcome message', () => { page.navigateTo(); - expect(page.getParagraphText()).toEqual('Welcome to angular ssr app!'); + expect(page.getParagraphText()).toEqual('Home page!'); }); }); diff --git a/project/application/src/app/app.component.html b/project/application/src/app/app.component.html index b651552..0680b43 100644 --- a/project/application/src/app/app.component.html +++ b/project/application/src/app/app.component.html @@ -1 +1 @@ -

{{ title }}

+ diff --git a/project/application/src/app/app.component.spec.ts b/project/application/src/app/app.component.spec.ts index 097d7b9..f1a4fb6 100644 --- a/project/application/src/app/app.component.spec.ts +++ b/project/application/src/app/app.component.spec.ts @@ -1,9 +1,13 @@ import { TestBed, async } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; + import { AppComponent } from './app.component'; + describe('AppComponent', () => { beforeEach( async(() => { TestBed.configureTestingModule({ + imports: [RouterTestingModule], declarations: [AppComponent], }).compileComponents(); }) @@ -24,13 +28,4 @@ describe('AppComponent', () => { expect(app.title).toEqual('Welcome to angular ssr app!'); }) ); - it( - 'should render title in a h1 tag', - async(() => { - const fixture = TestBed.createComponent(AppComponent); - fixture.detectChanges(); - const compiled = fixture.debugElement.nativeElement; - expect(compiled.querySelector('h1').textContent).toContain('Welcome to angular ssr app!'); - }) - ); }); diff --git a/project/application/src/app/app.config.ts b/project/application/src/app/app.config.ts deleted file mode 100644 index 15111bb..0000000 --- a/project/application/src/app/app.config.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { environment } from '../environments/environment'; - -export const AppConfig: any = environment; diff --git a/project/application/src/app/app.module.ts b/project/application/src/app/app.module.ts index 8727bd5..96e145d 100644 --- a/project/application/src/app/app.module.ts +++ b/project/application/src/app/app.module.ts @@ -1,20 +1,13 @@ import { NgModule } from '@angular/core'; -import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser'; -import { PLATFORM_ID, APP_ID, Inject } from '@angular/core'; -import { isPlatformBrowser } from '@angular/common'; +import { RouterModule } from '@angular/router'; -import { AppComponent } from '@angular-universal/app.component'; +import { AppComponent } from './app.component'; +import { CoreModule } from './modules/core/core.module'; +import { SharedModule } from './modules/shared/shared.module'; @NgModule({ - imports: [BrowserModule.withServerTransition({ appId: 'angular-universal' }), BrowserTransferStateModule], + imports: [RouterModule, CoreModule, SharedModule], declarations: [AppComponent], - providers: [], bootstrap: [AppComponent], }) -export class AppModule { - constructor(@Inject(PLATFORM_ID) private platformId: Object, @Inject(APP_ID) private appId: string) { - const platform = isPlatformBrowser(platformId) ? 'in the browser' : 'on the server'; - - console.log(`Running ${platform} with appId=${appId}`); - } -} +export class AppModule {} diff --git a/project/application/src/app/app.server.module.ts b/project/application/src/app/app.server.module.ts index 602c63a..3ed2c57 100644 --- a/project/application/src/app/app.server.module.ts +++ b/project/application/src/app/app.server.module.ts @@ -2,8 +2,8 @@ import { NgModule } from '@angular/core'; import { ServerModule, ServerTransferStateModule } from '@angular/platform-server'; import { ModuleMapLoaderModule } from '@nguniversal/module-map-ngfactory-loader'; -import { AppModule } from '@angular-universal/app.module'; -import { AppComponent } from '@angular-universal/app.component'; +import { AppModule } from './app.module'; +import { AppComponent } from './app.component'; @NgModule({ imports: [AppModule, ServerModule, ModuleMapLoaderModule, ServerTransferStateModule], diff --git a/project/application/src/app/configs/app-settings.config.ts b/project/application/src/app/configs/app-settings.config.ts new file mode 100644 index 0000000..f95ce62 --- /dev/null +++ b/project/application/src/app/configs/app-settings.config.ts @@ -0,0 +1,3 @@ +import { environment } from '../../environments/environment'; + +export const AppSettingsConfig: any = environment; diff --git a/project/application/src/app/modules/core/authentication/.gitkeep b/project/application/src/app/modules/core/authentication/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/core/core.module.ts b/project/application/src/app/modules/core/core.module.ts new file mode 100644 index 0000000..f20332f --- /dev/null +++ b/project/application/src/app/modules/core/core.module.ts @@ -0,0 +1,13 @@ +import { NgModule } from '@angular/core'; +import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser'; + +import { RoutingModule } from '../routing/routing.module'; + +@NgModule({ + imports: [ + BrowserModule.withServerTransition({ appId: 'angular-universal' }), + BrowserTransferStateModule, + RoutingModule, + ], +}) +export class CoreModule {} diff --git a/project/application/src/app/modules/core/guards/.gitkeep b/project/application/src/app/modules/core/guards/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/core/http/.gitkeep b/project/application/src/app/modules/core/http/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/core/interceptors/.gitkeep b/project/application/src/app/modules/core/interceptors/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/core/mocks/.gitkeep b/project/application/src/app/modules/core/mocks/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.spec.ts b/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.spec.ts new file mode 100644 index 0000000..894c037 --- /dev/null +++ b/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.spec.ts @@ -0,0 +1,18 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { HttpResponseStatusService } from './http-response-status.service'; + +describe('HttpResponseStatusService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [HttpResponseStatusService], + }); + }); + + it( + 'should be created', + inject([HttpResponseStatusService], (service: HttpResponseStatusService) => { + expect(service).toBeTruthy(); + }) + ); +}); diff --git a/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.ts b/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.ts new file mode 100644 index 0000000..31e74a8 --- /dev/null +++ b/project/application/src/app/modules/core/services/http-response-status/http-response-status.service.ts @@ -0,0 +1,22 @@ +import { Inject, Injectable, Optional } from '@angular/core'; +import { RESPONSE } from '@nguniversal/express-engine/tokens'; + +import { CoreModule } from '../../core.module'; + +@Injectable({ + providedIn: CoreModule, +}) +export class HttpResponseStatusService { + constructor( + @Optional() + @Inject(RESPONSE) + private res: any + ) {} + + public setStatus(code: number, message: string): void { + if (this.res) { + this.res.statusCode = code; + this.res.statusMessage = message; + } + } +} diff --git a/project/application/src/app/modules/home/components/.gitkeep b/project/application/src/app/modules/home/components/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/home/home-routing.module.ts b/project/application/src/app/modules/home/home-routing.module.ts new file mode 100644 index 0000000..cfb2efb --- /dev/null +++ b/project/application/src/app/modules/home/home-routing.module.ts @@ -0,0 +1,16 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; + +import { HomeComponent } from './home.component'; + +export const routes: Routes = [ + { + path: '', + component: HomeComponent, + }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], +}) +export class HomeRoutingModule {} diff --git a/project/application/src/app/modules/home/home.component.css b/project/application/src/app/modules/home/home.component.css new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/home/home.component.html b/project/application/src/app/modules/home/home.component.html new file mode 100644 index 0000000..b651552 --- /dev/null +++ b/project/application/src/app/modules/home/home.component.html @@ -0,0 +1 @@ +

{{ title }}

diff --git a/project/application/src/app/modules/home/home.component.spec.ts b/project/application/src/app/modules/home/home.component.spec.ts new file mode 100644 index 0000000..b446d09 --- /dev/null +++ b/project/application/src/app/modules/home/home.component.spec.ts @@ -0,0 +1,36 @@ +import { TestBed, async } from '@angular/core/testing'; + +import { HomeComponent } from './home.component'; + +describe('HomeComponent', () => { + beforeEach( + async(() => { + TestBed.configureTestingModule({ declarations: [HomeComponent] }).compileComponents(); + }) + ); + it( + 'should create the app', + async(() => { + const fixture = TestBed.createComponent(HomeComponent); + const app = fixture.debugElement.componentInstance; + expect(app).toBeTruthy(); + }) + ); + it( + `should have as title 'Home page!'`, + async(() => { + const fixture = TestBed.createComponent(HomeComponent); + const app = fixture.debugElement.componentInstance; + expect(app.title).toEqual('Home page!'); + }) + ); + it( + 'should render title in a h1 tag', + async(() => { + const fixture = TestBed.createComponent(HomeComponent); + fixture.detectChanges(); + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('Home page!'); + }) + ); +}); diff --git a/project/application/src/app/modules/home/home.component.ts b/project/application/src/app/modules/home/home.component.ts new file mode 100644 index 0000000..a96e724 --- /dev/null +++ b/project/application/src/app/modules/home/home.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-home', + templateUrl: './home.component.html', + styleUrls: ['./home.component.css'], +}) +export class HomeComponent { + title = 'Home page!'; +} diff --git a/project/application/src/app/modules/home/home.module.ts b/project/application/src/app/modules/home/home.module.ts new file mode 100644 index 0000000..4ad471b --- /dev/null +++ b/project/application/src/app/modules/home/home.module.ts @@ -0,0 +1,10 @@ +import { NgModule } from '@angular/core'; + +import { HomeRoutingModule } from './home-routing.module'; +import { HomeComponent } from './home.component'; + +@NgModule({ + imports: [HomeRoutingModule], + declarations: [HomeComponent], +}) +export class HomeModule {} diff --git a/project/application/src/app/modules/not-found/components/.gitkeep b/project/application/src/app/modules/not-found/components/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/not-found/not-found-routing.module.ts b/project/application/src/app/modules/not-found/not-found-routing.module.ts new file mode 100644 index 0000000..c3890e9 --- /dev/null +++ b/project/application/src/app/modules/not-found/not-found-routing.module.ts @@ -0,0 +1,16 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; + +import { NotFoundComponent } from './not-found.component'; + +export const routes: Routes = [ + { + path: '', + component: NotFoundComponent, + }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], +}) +export class NotFoundRoutingModule {} diff --git a/project/application/src/app/modules/not-found/not-found.component.css b/project/application/src/app/modules/not-found/not-found.component.css new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/not-found/not-found.component.html b/project/application/src/app/modules/not-found/not-found.component.html new file mode 100644 index 0000000..b651552 --- /dev/null +++ b/project/application/src/app/modules/not-found/not-found.component.html @@ -0,0 +1 @@ +

{{ title }}

diff --git a/project/application/src/app/modules/not-found/not-found.component.spec.ts b/project/application/src/app/modules/not-found/not-found.component.spec.ts new file mode 100644 index 0000000..d02f12e --- /dev/null +++ b/project/application/src/app/modules/not-found/not-found.component.spec.ts @@ -0,0 +1,41 @@ +import { TestBed, async } from '@angular/core/testing'; +import { RouterTestingModule } from '@angular/router/testing'; + +import { NotFoundComponent } from './not-found.component'; +import { CoreModule } from '../core/core.module'; + +describe('NotFoundComponent', () => { + beforeEach( + async(() => { + TestBed.configureTestingModule({ + declarations: [NotFoundComponent], + imports: [CoreModule, RouterTestingModule], + }).compileComponents(); + }) + ); + it( + 'should create the app', + async(() => { + const fixture = TestBed.createComponent(NotFoundComponent); + const app = fixture.debugElement.componentInstance; + expect(app).toBeTruthy(); + }) + ); + it( + `should have as title 'Not found page!'`, + async(() => { + const fixture = TestBed.createComponent(NotFoundComponent); + const app = fixture.debugElement.componentInstance; + expect(app.title).toEqual('Not found page!'); + }) + ); + it( + 'should render title in a h1 tag', + async(() => { + const fixture = TestBed.createComponent(NotFoundComponent); + fixture.detectChanges(); + const compiled = fixture.debugElement.nativeElement; + expect(compiled.querySelector('h1').textContent).toContain('Not found page!'); + }) + ); +}); diff --git a/project/application/src/app/modules/not-found/not-found.component.ts b/project/application/src/app/modules/not-found/not-found.component.ts new file mode 100644 index 0000000..6ca3657 --- /dev/null +++ b/project/application/src/app/modules/not-found/not-found.component.ts @@ -0,0 +1,18 @@ +import { Component, OnInit } from '@angular/core'; + +import { HttpResponseStatusService } from '../core/services/http-response-status/http-response-status.service'; + +@Component({ + selector: 'app-not-found', + templateUrl: './not-found.component.html', + styleUrls: ['./not-found.component.css'], +}) +export class NotFoundComponent implements OnInit { + title = 'Not found page!'; + + constructor(private httpResponseStatusService: HttpResponseStatusService) {} + + ngOnInit() { + this.httpResponseStatusService.setStatus(404, 'Not Found'); + } +} diff --git a/project/application/src/app/modules/not-found/not-found.module.ts b/project/application/src/app/modules/not-found/not-found.module.ts new file mode 100644 index 0000000..44cd9f0 --- /dev/null +++ b/project/application/src/app/modules/not-found/not-found.module.ts @@ -0,0 +1,10 @@ +import { NgModule } from '@angular/core'; + +import { NotFoundComponent } from './not-found.component'; +import { NotFoundRoutingModule } from './not-found-routing.module'; + +@NgModule({ + imports: [NotFoundRoutingModule], + declarations: [NotFoundComponent], +}) +export class NotFoundModule {} diff --git a/project/application/src/app/modules/routing/routing.module.ts b/project/application/src/app/modules/routing/routing.module.ts new file mode 100644 index 0000000..910f359 --- /dev/null +++ b/project/application/src/app/modules/routing/routing.module.ts @@ -0,0 +1,19 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; + +export const routes: Routes = [ + { + path: '', + loadChildren: '../home/home.module#HomeModule', + }, + { + path: '**', + loadChildren: '../not-found/not-found.module#NotFoundModule', + }, +]; + +@NgModule({ + imports: [RouterModule.forRoot(routes, { initialNavigation: 'enabled' })], + exports: [RouterModule], +}) +export class RoutingModule {} diff --git a/project/application/src/app/modules/shared/components/.gitkeep b/project/application/src/app/modules/shared/components/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/shared/directives/.gitkeep b/project/application/src/app/modules/shared/directives/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/shared/pipes/.gitkeep b/project/application/src/app/modules/shared/pipes/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/project/application/src/app/modules/shared/shared.module.ts b/project/application/src/app/modules/shared/shared.module.ts new file mode 100644 index 0000000..dcce5fb --- /dev/null +++ b/project/application/src/app/modules/shared/shared.module.ts @@ -0,0 +1,4 @@ +import { NgModule } from '@angular/core'; + +@NgModule({}) +export class SharedModule {} diff --git a/project/application/tsconfig.json b/project/application/tsconfig.json index 2f4f600..25253fe 100644 --- a/project/application/tsconfig.json +++ b/project/application/tsconfig.json @@ -10,9 +10,6 @@ "target": "es5", "typeRoots": ["node_modules/@types"], "lib": ["es2017", "dom"], - "baseUrl": "src", - "paths": { - "@angular-universal/*": ["app/*"] - } + "baseUrl": "./", } }