-
Notifications
You must be signed in to change notification settings - Fork 3
/
express.js
36 lines (29 loc) · 1.41 KB
/
express.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
const express = require('express'); // eslint-disable-line
const compression = require('compression'); // eslint-disable-line
const browserCapabilities = require('browser-capabilities'); // eslint-disable-line
const UAParser = require('ua-parser-js').UAParser; // eslint-disable-line
const app = express();
const basedir = __dirname + '/src/'; // eslint-disable-line
function getSourcesPath(request, filePath = '') {
const userAgent = request.headers['user-agent'];
const clientCapabilities = browserCapabilities.browserCapabilities(userAgent);
const browserName = new UAParser(userAgent).getBrowser().name || '';
// skip Edge because browser-capabilities library is outdated
const needToUpgrade = !clientCapabilities.has('modules') && browserName !== 'Edge';
return needToUpgrade ? `${basedir}upgrade-browser.html` : `${basedir}${filePath}`;
}
app.use(compression());
app.use('/pmp/', (req, res, next) => {
express.static(getSourcesPath(req))(req, res, next);
});
app.get(/.*service-worker\.js/, (req, res) => {
res.sendFile(getSourcesPath(req, 'service-worker.js'));
});
app.use((req, res) => {
// handles app access using a different state path than index (otherwise it will not return any file)
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
res.sendFile(getSourcesPath(req, 'index.html'));
});
app.listen(8080);