-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpress.js
More file actions
40 lines (32 loc) · 1.33 KB
/
express.js
File metadata and controls
40 lines (32 loc) · 1.33 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
import express from 'express';
import compression from 'compression';
import browserCapabilities from 'browser-capabilities';
import {UAParser} from 'ua-parser-js';
import {fileURLToPath} from 'url';
import {dirname} from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const basedir = __dirname + '/src/';
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('/menu/', (req, res, next) => {
express.static(getSourcesPath(req))(req, res, next);
});
app.get(/.*service-worker\.js/, function (req, res) {
res.sendFile(getSourcesPath(req) + 'service-worker.js');
});
app.use((req, res) => {
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);