This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
133 lines (111 loc) · 3.31 KB
/
server.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
'use strict';
/* Server specific version of Zone.js */
import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import * as express from 'express';
import * as helmet from 'helmet';
import * as proxy from 'http-proxy-middleware';
import * as xhr2 from 'xhr2';
import { ngExpressEngine } from '@nguniversal/express-engine';
import { join } from 'path';
import {v4} from 'uuid';
import {PREBOOT_NONCE} from 'preboot';
xhr2.prototype._restrictedHeaders = {};
const DIST_FOLDER = join(process.cwd(), 'dist');
const PORT = process.env.FRONTEND_PORT || 3000;
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
const app = express();
const devServerProtocol = 'http';
const devWsProtocol = 'ws';
const devServerHost = 'localhost';
const devServerPort = 4000;
const devServer = `${devServerProtocol}://${devServerHost}:${devServerPort}`;
const devWs = `${devWsProtocol}://${devServerHost}:${devServerPort}`;
const devWsProxy = proxy('/wss', {
target: devWs,
logLevel: 'debug',
pathRewrite: {
'^/wss': ''
},
ws: true,
});
const devServerProxy = proxy('/data', {
target: devServer,
logLevel: 'debug',
pathRewrite: {
'^/data': ''
}
});
app.use(devServerProxy);
app.use(devWsProxy);
app.use((req, res, next) => {
res.locals.nonce = v4();
next();
});
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: [`'none'`],
styleSrc: [`'self'`, 'fonts.googleapis.com', `'unsafe-inline'`],
workerSrc: [`'self'`],
scriptSrc: [`'self'`, `'unsafe-eval'`, (req, res) => `'nonce-${ res.locals.nonce }'`],
connectSrc: [
`'self'`,
'fonts.gstatic.com',
'fonts.googleapis.com',
(req, res) => `wss://${req.get('host')}/wss/subscriptions`,
(req, res) => `ws://${req.get('host')}/wss/subscriptions`,
],
imgSrc: [`'self'`, 'data:'],
manifestSrc: [`'self'`],
fontSrc: [`'self'`, 'fonts.gstatic.com']
}
}));
app.use(helmet.frameguard({ action: 'deny' }));
const sixtyDaysInSeconds = 5184000;
app.use(helmet.hsts({
maxAge: sixtyDaysInSeconds
}));
/* Server-side rendering */
function angularRouter(req, res) {
/* Server-side rendering */
res.render('index', {
req,
res,
providers: [
{
provide: PREBOOT_NONCE,
useValue: res.locals.nonce
}
]
}, function (error, html) {
if (error) {
console.error('rendering error', error);
}
res.send(html);
});
}
/* Root route before static files, or it will serve a static index.html, without pre-rendering */
app.get('/', angularRouter);
// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
maxAge: '1y'
}));
app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
/* - Example Express Rest API endpoints -
app.get('/api/**', (req, res) => { });
*/
// ALl regular routes use the Universal engine
app.get('*', angularRouter);
// Start up the Node server
app.listen(PORT, () => {
console.log(`Node Express server listening on http://localhost:${PORT}`);
});