forked from ionic-team/ionic-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (53 loc) · 1.71 KB
/
server.js
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
const express = require('express');
const parseurl = require('parseurl');
const path = require('path');
const Sentry = require('@sentry/node');
const compress = require('compression');
const helmet = require('helmet');
const throng = require('throng');
const start = () => {
const app = express();
app.set('trust proxy', true);
if (process.env.SENTRY_DSN) {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENVIRONMENT
});
}
app.use(Sentry.Handlers.requestHandler());
app.use(compress());
app.use(helmet());
app.get('/', (_, res) => res.redirect(301, '/docs/'));
app.use((req, res, next) => {
const pathname = path.join(parseurl(req).pathname, 'index.html');
const options = { root: path.join(__dirname, 'www') };
res.sendFile(pathname, options, err => err && next());
});
app.use(express.static(path.join(__dirname, 'www'), {
maxAge: '2h',
setHeaders: setCustomCacheControl
}));
app.use(Sentry.Handlers.errorHandler());
app.use((_, res) => {
res.status(404).sendFile(`${__dirname}/www/docs/index.html`);
});
const listener = app.listen(process.env.PORT || 3030, () => {
console.log(`Listening on port ${listener.address().port}`);
});
function setCustomCacheControl (res, path) {
if (
express.static.mime.lookup(path) === 'text/javascript' &&
path.indexOf('p-') !== -1
) {
// Custom Cache-Control for production JS files
res.setHeader('Cache-Control', 'public, max-age=86400')
} else {
res.setHeader('Cache-Control', 'public, max-age=600')
}
}
}
throng({
workers: process.env.WEB_CONCURRENCY ?
parseInt(process.env.WEB_CONCURRENCY, 10) : 1,
lifetime: Infinity
}, start)