-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ssr.ts
133 lines (118 loc) · 3.82 KB
/
server.ssr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* *** NOTE ON IMPORTING FROM ANGULAR AND NGUNIVERSAL IN THIS FILE ***
*
* If your application uses third-party dependencies, you'll need to
* either use Webpack or the Angular CLI's `bundleDependencies` feature
* in order to adequately package them for use on the server without a
* node_modules directory.
*
* However, due to the nature of the CLI's `bundleDependencies`, importing
* Angular in this file will create a different instance of Angular than
* the version in the compiled application code. This leads to unavoidable
* conflicts. Therefore, please do not explicitly import from @angular or
* @nguniversal in this file. You can export any needed resources
* from your application's main.server.ts file, as seen below with the
* import for `ngExpressEngine`.
*/
import 'zone.js/dist/zone-node';
import * as express from 'express';
import { join } from 'path';
import { APP_BASE_HREF } from '@angular/common';
import 'universal-dotenv/register';
/**
* This polyfills node with additional locales
*/
if (global.Intl) {
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
} else {
global.Intl = require('intl');
}
// Express app
function app() {
const server = express();
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
AppServerModuleNgFactory,
LAZY_MODULE_MAP,
ngExpressEngine,
provideModuleMap,
addCorsMiddleware,
addAuthMiddleware,
addBapiProxyMiddleware,
addContentMiddleware,
addLocaleMiddleware,
addCookieMiddleware,
addPanelMiddleware,
addBasketApi,
addSecurityMiddleware,
addCheckoutApi,
addAuthApi,
addReservationsApi,
addInvoiceApi,
addOrdersApi,
EXPRESS_REQUEST,
} = require('./dist/server/main');
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', (_, options: Request, callback) => {
const engine = ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
// tslint:disable-next-line: no-string-literal
{ provide: EXPRESS_REQUEST, useFactory: () => options['req'], deps: [] },
provideModuleMap(LAZY_MODULE_MAP),
],
});
engine(_, options, callback);
});
server.set('view engine', 'html');
server.set('views', DIST_FOLDER);
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get(
'*.*',
express.static(DIST_FOLDER, {
maxAge: '1y',
}),
);
// Express middlewares
// WARNING: If you like to make changes here consider the correct order
addCorsMiddleware(server);
addCookieMiddleware(server);
// addSecurityMiddleware(server);
addAuthMiddleware(server);
addBapiProxyMiddleware(server);
addContentMiddleware(server);
addPanelMiddleware(server);
addLocaleMiddleware(server);
addBasketApi(server);
addCheckoutApi(server);
addAuthApi(server);
addOrdersApi(server);
addReservationsApi(server);
addInvoiceApi(server);
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render('index', { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run() {
const PORT = process.env.PORT || 80;
// Start up the Node server
const server = app();
server.listen(PORT, () => {
console.log(`Node Express server listening on PORT: ${PORT}`);
});
process.on('uncaughtException', error => {
console.log('Something unknown happened: ', error);
});
process.on('unhandledRejection', (error, promise) => {
console.log('Got an unhandled promise error: ', promise);
console.log('The error was: ', error);
});
}
// Start the server
run();