1 |
44383
|
argiro.kok
|
// the polyfills must be one of the first things imported in node.js.
|
2 |
|
|
// The only modules to be imported higher - node modules with es6-promise 3.x or other Promise polyfill dependency
|
3 |
|
|
// (rule of thumb: do it if you have zone.js exception that it has been overwritten)
|
4 |
|
|
// if you are including modules that modify Promise, such as NewRelic,, you must include them before polyfills
|
5 |
43769
|
argiro.kok
|
import 'angular2-universal-polyfills';
|
6 |
44619
|
argiro.kok
|
import 'ts-helpers';
|
7 |
|
|
import './__workaround.node'; // temporary until 2.1.1 things are patched in Core
|
8 |
43769
|
argiro.kok
|
|
9 |
|
|
import * as path from 'path';
|
10 |
|
|
import * as express from 'express';
|
11 |
|
|
import * as bodyParser from 'body-parser';
|
12 |
|
|
import * as cookieParser from 'cookie-parser';
|
13 |
44726
|
argiro.kok
|
import * as morgan from 'morgan';
|
14 |
44619
|
argiro.kok
|
import * as compression from 'compression';
|
15 |
43769
|
argiro.kok
|
|
16 |
|
|
// Angular 2
|
17 |
|
|
import { enableProdMode } from '@angular/core';
|
18 |
|
|
// Angular 2 Universal
|
19 |
|
|
import { createEngine } from 'angular2-express-engine';
|
20 |
|
|
|
21 |
|
|
// App
|
22 |
44619
|
argiro.kok
|
import { MainModule } from './node.module';
|
23 |
43769
|
argiro.kok
|
|
24 |
44619
|
argiro.kok
|
// Routes
|
25 |
|
|
import { routes } from './server.routes';
|
26 |
|
|
|
27 |
43769
|
argiro.kok
|
// enable prod for faster renders
|
28 |
|
|
enableProdMode();
|
29 |
|
|
|
30 |
|
|
const app = express();
|
31 |
|
|
const ROOT = path.join(path.resolve(__dirname, '..'));
|
32 |
|
|
|
33 |
|
|
// Express View
|
34 |
44383
|
argiro.kok
|
app.engine('.html', createEngine({
|
35 |
|
|
ngModule: MainModule,
|
36 |
|
|
providers: [
|
37 |
44619
|
argiro.kok
|
// use only if you have shared state between users
|
38 |
|
|
// { provide: 'LRU', useFactory: () => new LRU(10) }
|
39 |
|
|
|
40 |
44383
|
argiro.kok
|
// stateless providers only since it's shared
|
41 |
|
|
]
|
42 |
|
|
}));
|
43 |
|
|
app.set('port', process.env.PORT || 3000);
|
44 |
43769
|
argiro.kok
|
app.set('views', __dirname);
|
45 |
|
|
app.set('view engine', 'html');
|
46 |
44619
|
argiro.kok
|
app.set('json spaces', 2);
|
47 |
43769
|
argiro.kok
|
|
48 |
|
|
app.use(cookieParser('Angular 2 Universal'));
|
49 |
|
|
app.use(bodyParser.json());
|
50 |
44619
|
argiro.kok
|
app.use(compression());
|
51 |
43769
|
argiro.kok
|
|
52 |
44726
|
argiro.kok
|
app.use(morgan('dev'));
|
53 |
|
|
|
54 |
44619
|
argiro.kok
|
function cacheControl(req, res, next) {
|
55 |
|
|
// instruct browser to revalidate in 60 seconds
|
56 |
|
|
res.header('Cache-Control', 'max-age=60');
|
57 |
|
|
next();
|
58 |
|
|
}
|
59 |
43769
|
argiro.kok
|
// Serve static files
|
60 |
44619
|
argiro.kok
|
app.use('/assets', cacheControl, express.static(path.join(__dirname, 'assets'), {maxAge: 30}));
|
61 |
|
|
app.use(cacheControl, express.static(path.join(ROOT, 'dist/client'), {index: false}));
|
62 |
43769
|
argiro.kok
|
|
63 |
44619
|
argiro.kok
|
//
|
64 |
|
|
/////////////////////////
|
65 |
|
|
// ** Example API
|
66 |
|
|
// Notice API should be in aseparate process
|
67 |
|
|
import { serverApi, createTodoApi } from './backend/api';
|
68 |
43769
|
argiro.kok
|
// Our API for demos only
|
69 |
|
|
app.get('/data.json', serverApi);
|
70 |
44619
|
argiro.kok
|
app.use('/api', createTodoApi());
|
71 |
43769
|
argiro.kok
|
|
72 |
45786
|
argiro.kok
|
process.on('uncaughtException', function (err) {
|
73 |
|
|
console.error('Catching uncaught errors to avoid process crash', err);
|
74 |
|
|
});
|
75 |
|
|
|
76 |
43769
|
argiro.kok
|
function ngApp(req, res) {
|
77 |
45786
|
argiro.kok
|
|
78 |
|
|
function onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
|
79 |
|
|
console.warn('Error in SSR, serving for direct CSR');
|
80 |
|
|
res.sendFile('index.html', {root: './src'});
|
81 |
|
|
return false;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => {
|
85 |
|
|
res.render('index', {
|
86 |
|
|
req,
|
87 |
|
|
res,
|
88 |
|
|
// time: true, // use this to determine what part of your app is slow only in development
|
89 |
|
|
preboot: false,
|
90 |
|
|
baseUrl: '/',
|
91 |
|
|
requestUrl: req.originalUrl,
|
92 |
|
|
originUrl: `http://localhost:${ app.get('port') }`
|
93 |
|
|
});
|
94 |
43769
|
argiro.kok
|
});
|
95 |
45786
|
argiro.kok
|
|
96 |
43769
|
argiro.kok
|
}
|
97 |
44619
|
argiro.kok
|
|
98 |
|
|
/**
|
99 |
|
|
* use universal for specific routes
|
100 |
|
|
*/
|
101 |
43769
|
argiro.kok
|
app.get('/', ngApp);
|
102 |
44619
|
argiro.kok
|
routes.forEach(route => {
|
103 |
|
|
app.get(`/${route}`, ngApp);
|
104 |
|
|
app.get(`/${route}/*`, ngApp);
|
105 |
|
|
});
|
106 |
|
|
app.get('*', ngApp);
|
107 |
|
|
/*app.get('*', function(req, res) {
|
108 |
44048
|
argiro.kok
|
res.setHeader('Content-Type', 'application/json');
|
109 |
44619
|
argiro.kok
|
var pojo = { status: 404, message: 'No Content' };
|
110 |
44048
|
argiro.kok
|
var json = JSON.stringify(pojo, null, 2);
|
111 |
44619
|
argiro.kok
|
res.status(404).send(json);
|
112 |
44048
|
argiro.kok
|
});
|
113 |
44619
|
argiro.kok
|
*/
|
114 |
43769
|
argiro.kok
|
// Server
|
115 |
44619
|
argiro.kok
|
let server = app.listen(app.get('port'), () => {
|
116 |
43769
|
argiro.kok
|
console.log(`Listening on: http://localhost:${server.address().port}`);
|
117 |
|
|
});
|