forked from mozilla/platform-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (45 loc) · 1.5 KB
/
app.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
const express = require('express');
const fs = require('fs');
const compression = require('compression');
const app = express();
const diggerAPI = require('./routes/api');
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const distPublicDir = './dist/public';
// forceHost
app.use((req, res, next) => {
const host = req.get('Host');
if (!/local|-pr-\d+\./.test(host) && host !== 'platform-status.mozilla.org') {
res.redirect(301, `https://platform-status.mozilla.org${req.url}`);
} else {
next();
}
});
// forceSSL
app.use((req, res, next) => {
const host = req.get('Host');
if (!host.startsWith('localhost')) {
// https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security
res.header('Strict-Transport-Security', 'max-age=15768000');
// https://github.com/rangle/force-ssl-heroku/blob/master/force-ssl-heroku.js
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(301, `https://${host}${req.url}`);
}
}
return next();
});
// corsify
app.use((req, res, next) => {
// http://enable-cors.org/server_expressjs.html
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept');
next();
});
app.use(compression());
if (!fs.existsSync(distPublicDir)) {
throw new Error('Missing `dist` folder, execute `npm run build` first.');
}
app.use(express.static(distPublicDir));
app.use(diggerAPI);
console.log('App is configured');
module.exports = app;