Skip to content

Commit 1d41210

Browse files
committed
2 parents b720066 + af7c5ea commit 1d41210

11 files changed

+129
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"core-js": "2.5.3",
4444
"ngx-cookie-service": "1.0.10",
4545
"rxjs": "5.5.6",
46+
"node-fetch": "^2.0.0",
4647
"zone.js": "0.8.20"
4748
},
4849
"devDependencies": {

server.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const path = require('path');
44
const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString();
55
const win = domino.createWindow(template);
66
const files = fs.readdirSync(`${process.cwd()}/dist-server`);
7+
import fetch from 'node-fetch';
78
// const styleFiles = files.filter(file => file.startsWith('styles'));
89
// const hashStyle = styleFiles[0].split('.')[1];
910
// const style = fs.readFileSync(path.join(__dirname, '.', 'dist-server', `styles.${hashStyle}.bundle.css`)).toString();
1011

12+
win.fetch = fetch;
1113
global['window'] = win;
1214
Object.defineProperty(win.document.body.style, 'transform', {
1315
value: () => {
@@ -97,24 +99,31 @@ app.get('*', (req, res) => {
9799

98100
// tslint:disable-next-line:no-console
99101
console.time(`GET: ${req.originalUrl}`);
100-
res.render('../dist/index', {
101-
req: req,
102-
res: res,
103-
providers: [
104-
{
105-
provide: REQUEST, useValue: (req)
106-
},
107-
{
108-
provide: RESPONSE, useValue: (res)
109-
},
110-
{
111-
provide: 'ORIGIN_URL',
112-
useValue: (`${http}://${req.headers.host}`)
113-
}
114-
]
115-
});
116-
// tslint:disable-next-line:no-console
117-
console.timeEnd(`GET: ${req.originalUrl}`);
102+
res.render(
103+
'../dist/index',
104+
{
105+
req: req,
106+
res: res,
107+
providers: [
108+
{
109+
provide: REQUEST, useValue: (req)
110+
},
111+
{
112+
provide: RESPONSE, useValue: (res)
113+
},
114+
{
115+
provide: 'ORIGIN_URL',
116+
useValue: (`${http}://${req.headers.host}`)
117+
}
118+
]
119+
},
120+
(err, html) => {
121+
if (!!err) throw err;
122+
123+
// tslint:disable-next-line:no-console
124+
console.timeEnd(`GET: ${req.originalUrl}`);
125+
res.send(html);
126+
});
118127
});
119128

120129
app.listen(PORT, () => {

src/app/app.routing.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MetaGuard } from '@ngx-meta/core';
33

44
const routes: Routes = [
55
{
6-
path: '', loadChildren: './home/home.module#HomeModule', pathMatch: 'full',
6+
path: '', loadChildren: './home/home.module#HomeModule',
77
data: {
88
// for override default meta
99
meta: {
@@ -19,6 +19,9 @@ const routes: Routes = [
1919
{ path: 'mock', loadChildren: './mock-server-browser/mock-server-browser.module#MockServerBrowserModule' },
2020
// with meta
2121
{ path: 'back', loadChildren: './transfer-back/transfer-back.module#TransferBackModule', canActivateChild: [MetaGuard]},
22+
// 404
23+
{ path: '404', loadChildren: './not-found/not-found.module#NotFoundModule' },
24+
{ path: '**', redirectTo: '404', pathMatch: 'full' }
2225
];
2326
// must use {initialNavigation: 'enabled'}) - for one load page, without reload
2427
export const AppRoutes = RouterModule.forRoot(routes, { initialNavigation: 'enabled' });
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
not-found works!
3+
</p>

src/app/not-found/not-found.component.scss

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { NotFoundComponent } from './not-found.component';
4+
5+
describe('NotFoundComponent', () => {
6+
let component: NotFoundComponent;
7+
let fixture: ComponentFixture<NotFoundComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ NotFoundComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(NotFoundComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { NotFoundService } from './not-found.service';
3+
4+
@Component({
5+
selector: 'app-not-found',
6+
templateUrl: './not-found.component.html',
7+
styleUrls: ['./not-found.component.scss']
8+
})
9+
export class NotFoundComponent implements OnInit {
10+
public status: { code: number, message: string };
11+
constructor(private _notFoundService: NotFoundService) {}
12+
13+
ngOnInit() {
14+
this._notFoundService.setStatus(404, 'Not Found');
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { NotFoundComponent } from './not-found.component';
4+
import { NotFoundRoutes } from './not-found.routing';
5+
import { NotFoundService } from './not-found.service';
6+
7+
@NgModule({
8+
imports: [
9+
CommonModule,
10+
NotFoundRoutes
11+
],
12+
providers: [NotFoundService],
13+
declarations: [NotFoundComponent]
14+
})
15+
export class NotFoundModule { }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { NotFoundComponent } from './not-found.component';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
const routes: Routes = [
5+
{ path: '' , component: NotFoundComponent},
6+
];
7+
8+
export const NotFoundRoutes = RouterModule.forChild(routes);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { NotFoundService } from './not-found.service';
4+
5+
describe('NotFoundService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [NotFoundService]
9+
});
10+
});
11+
12+
it('should be created', inject([NotFoundService], (service: NotFoundService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});

0 commit comments

Comments
 (0)