forked from jsdelivr/bootstrapcdn
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
236 lines (202 loc) · 6.24 KB
/
app.js
File metadata and controls
236 lines (202 loc) · 6.24 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
'use strict';
const path = require('path');
const express = require('express');
const mime = require('mime');
const uuidv4 = require('uuid/v4');
const semver = require('semver');
// constants
const ENV = process.env;
const NODE_ENV = ENV.NODE_ENV || 'development';
const PUBLIC_DIR = path.join(__dirname, 'public');
const customMime = mime.define({
'text/javascript': ['js']
}, true);
const STATIC_OPTS = {
maxAge: '1y',
lastModified: true,
etag: false,
mime: customMime
};
// middleware
const compression = require('compression');
const favicon = require('serve-favicon');
const logger = require('morgan');
const errorHandler = require('errorhandler');
const enforce = require('express-sslify');
const sitemap = require('express-sitemap');
const helmet = require('helmet');
const Rollbar = require('rollbar');
const staticify = require('staticify')(PUBLIC_DIR, {
sendOptions: STATIC_OPTS
});
const config = require('./config');
const CSP = require('./config/helmet-csp');
const helpers = require('./lib/helpers');
const routes = require('./routes');
const app = express();
// all environments
app.set('views', path.join(__dirname, '/views/'));
app.set('view engine', 'pug');
app.set('etag', false);
app.set('json escape', true);
app.set('json spaces', 2);
app.set('x-powered-by', false);
// Enable rollbar early on the middleware stack, if it's configured.
/* istanbul ignore if */
if (ENV.ROLLBAR_ACCESS_TOKEN) {
const rollbarOptions = {
accessToken: ENV.ROLLBAR_ACCESS_TOKEN,
environment: NODE_ENV,
captureUncaught: true,
captureUnhandledRejections: true
};
// Heroku sets this by default, so using it if present.
if (ENV.ROLLBAR_ENDPOINT) {
rollbarOptions.endpoint = ENV.ROLLBAR_ENDPOINT;
}
const rollbar = new Rollbar(rollbarOptions);
app.use(rollbar.errorHandler());
} else if (NODE_ENV !== 'test') {
console.log('WARNING: starting without rollbar');
}
/* istanbul ignore if */
if (NODE_ENV === 'production') {
// production
app.use(logger('combined'));
if (ENV.FORCE_SSL === 'true') {
// Because this is (always) going to break local, I'm requiring a secondary
// environment variable to be enabled to activate it. This is required in
// app.json to ensure that it's always set when building out the app on
// Heroku.
app.use(enforce.HTTPS({ trustProtoHeader: true }));
}
} else {
// development
app.locals.pretty = true;
app.use(logger('dev'));
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
}
// middleware
app.use(compression());
app.use(staticify.middleware);
app.use(favicon(path.join(PUBLIC_DIR, config.app.favicon.uri), '7d'));
app.use((req, res, next) => {
// Create a nonce for use with CSP;
// get a random UUID and convert it to a base64 string
const nonce = Buffer.from(uuidv4(), 'utf-8').toString('base64');
// make config available in routes
req.config = config;
// custom headers
res.setHeader('Accept-Ranges', 'bytes');
res.setHeader('Cache-Control', 'public, max-age=0');
res.setHeader('Last-Modified', new Date().toUTCString());
res.setHeader('X-Hello-Human', 'Say hello back! @getBootstrapCDN on Twitter');
res.setHeader('X-Powered-By', 'StackPath');
res.locals.nonce = nonce;
next();
});
app.use(express.static(PUBLIC_DIR, STATIC_OPTS));
app.use(helmet({
dnsPrefetchControl: false,
frameguard: {
action: 'deny'
}
}));
app.use(helmet.hsts({
force: true,
includeSubDomains: true,
maxAge: 63072000, // 2 years
preload: true
}));
app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));
app.use(helmet.contentSecurityPolicy({
directives: CSP,
// Set to false if you want to completely disable any user-agent sniffing.
// This may make the headers less compatible but it will be much faster.
// This defaults to `true`.
browserSniff: false
}));
// locals
app.locals.helpers = helpers;
app.locals.config = config;
app.locals.basedir = PUBLIC_DIR;
app.locals.getVersionedPath = staticify.getVersionedPath;
app.locals.semver = semver;
// routes
app.use('/', routes.indexRoute);
app.use('/about/', routes.aboutRoute);
app.use('/alpha/?|/beta/?', routes.redirectToRoot);
app.use('/books/', routes.booksRoute);
app.use('/bootlint/', routes.bootlintRoute);
app.use('/bootswatch/', routes.bootswatchRoute);
app.use('/bootswatch4/', routes.bootswatch4Route);
app.use('/data/bootstrapcdn.json', routes.dataRoute);
app.use('/fontawesome/', routes.fontawesomeRoute);
app.use('/integrations/', routes.integrationsRoute);
app.use('/jobs/', routes.jobsRoute);
app.use('/legacy', routes.legacyRoutes);
app.use('/privacy-policy/', routes.privacyPolicyRoute);
app.use('/showcase/', routes.showcaseRoute);
app.use('/themes/', routes.themesRoute);
const map = sitemap({
url: 'www.bootstrapcdn.com',
http: 'https',
sitemapSubmission: '/sitemap.xml',
cache: 60000, // enable 1m cache
route: { // custom route
'/': {
disallow: !ENV.ENABLE_CRAWLING
},
'/data/bootstrapcdn.json': {
hide: true // exclude this route from xml and txt
},
'/404/': {
hide: true
},
'/books/': {
hide: true
},
'/bootswatch4/': {
hide: true
},
'/jobs/': {
hide: true
},
'/legacy/': {
hide: true
},
'/robots.txt': {
hide: true
},
'/sitemap.xml': {
hide: true
},
'/themes/': {
hide: true
}
}
});
/* istanbul ignore if */
if (ENV.ENABLE_CRAWLING) {
app.get('/sitemap.xml', (req, res) => {
map.generate4(app, [
'/',
'/about',
'/bootlint',
'/bootswatch',
'/fontawesome',
'/integrations',
'/legacy',
'/privacy-policy',
'/showcase'
]);
return map.XMLtoWeb(res);
});
}
app.get('/robots.txt', (req, res) => map.TXTtoWeb(res));
app.use('*', routes.notFoundRoute);
module.exports = app;
// vim: ft=javascript sw=4 sts=4 et: