This repository has been archived by the owner on Jun 4, 2019. It is now read-only.
/
server.app.ts
130 lines (118 loc) · 3.4 KB
/
server.app.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
// tslint:disable:no-require-imports
import 'reflect-metadata'
import 'zone.js/dist/zone-node'
import 'zone.js/dist/long-stack-trace-zone'
import * as express from 'express'
import * as cookieParser from 'cookie-parser'
import { createLogger } from '@expo/bunyan'
import { ngExpressEngine } from '@nguniversal/express-engine'
import { AppServerModule } from './angular/server.angular.module'
import { argv } from 'yargs'
import { resolve } from 'path'
import { rollbarInit } from './add-ons/rollbar'
import { useApi } from './rest-api/index'
import ms = require('ms')
const shrinkRay = require('shrink-rayed')
const minifyHTML = require('express-minify-html')
const bunyanMiddleware = require('bunyan-middleware')
const xhr2 = require('xhr2')
const cors = require('cors')
// tslint:disable-next-line:no-object-mutation
xhr2.prototype._restrictedHeaders.cookie = false
require('ts-node/register')
const app = express()
rollbarInit(app)
const isProd = argv['build-type'] === 'prod' || argv['prod']
const isEndToEndTest = argv.e2e
const staticOptions = {
index: false,
maxAge: isProd ? ms('1y') : ms('0'),
setHeaders: (res: express.Response, path: any) => {
res.setHeader(
'Expires',
isProd
? new Date(Date.now() + ms('1y')).toUTCString()
: new Date(Date.now() + ms('0')).toUTCString()
)
}
}
!isEndToEndTest &&
app.use(
bunyanMiddleware({
logger: createLogger({
name: 'Fusing-Angular',
type: 'node-express'
}),
excludeHeaders: ['authorization', 'cookie']
})
)
const dir = resolve('dist')
app.engine('html', ngExpressEngine({ bootstrap: AppServerModule }))
app.set('ignore-routes', ['/api/'])
app.set('view engine', 'html')
app.set('views', dir)
app.use(cookieParser())
app.use(shrinkRay())
app.use(cors())
app.use(
minifyHTML({
override: true,
exception_url: false,
htmlMinifier: {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: false,
minifyJS: true
}
})
)
app.use('/css', express.static(`${dir}/css`, staticOptions))
app.use('/js', express.static(`${dir}/js`, staticOptions))
app.use('/ngsw.json', express.static(`${dir}/ngsw.json`, staticOptions))
app.use(
'/ngsw-worker.js',
express.static(`${dir}/ngsw-worker.js`, staticOptions)
)
app.use('/robots.txt', express.static(`${dir}/web/robots.txt`, staticOptions))
app.use('/ping.html', express.static(`${dir}/web/ping.html`, staticOptions))
app.use(
'/favicon.ico',
express.static(`${dir}/assets/favicons/favicon-16x16.png`, {
...staticOptions,
fallthrough: false
})
)
app.use(
'/assets/favicons/favicon.ico',
express.static(`${dir}/assets/favicons/favicon-16x16.png`, {
...staticOptions,
fallthrough: false
})
)
app.use(
'/assets',
express.static(`${dir}/assets`, { ...staticOptions, fallthrough: false })
)
app.use(
'/manifest.json',
express.static(`${dir}/assets`, { ...staticOptions, fallthrough: false })
)
app.get('/ad-server.js', (req, res) => res.send({}))
useApi(app)
app.set('ignore-routes', ['/api/', '/css/', '/js/', '/assets/'])
app.get(
'**',
(req: express.Request, res: express.Response, next: express.NextFunction) => {
const bypassed = (req.app.get('ignore-routes') as ReadonlyArray<
string
>).some(a => req.url.includes(a))
return bypassed
? next()
: res.render('index', {
req,
res
})
}
)
export { app }