forked from altair-graphql/altair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (48 loc) · 1.51 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
'use strict';
let fs = require('fs');
let path = require('path');
let express = require('express');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', (req, res) => {
let appHtml = fs.readFileSync(path.join(__dirname, 'dist/index.html'), 'utf8');
const crowdinScriptTags = `
<script type="text/javascript">
var _jipt = [];
_jipt.push(['project', 'altair-gql']);
</script>
<script type="text/javascript" src="//cdn.crowdin.com/jipt/jipt.js"></script>
`;
const translationFlagTag = `<script>window.__ALTAIR_TRANSLATE__ = 1;</script>`;
if (process.argv[2] === '--translate') {
appHtml = appHtml.replace('</head>', `${crowdinScriptTags}</head>`);
appHtml = appHtml.replace('</head>', `${translationFlagTag}</head>`);
}
res.send(appHtml);
});
app.use(express.static(path.join(__dirname, 'dist')));
const forceSSL = function() {
return function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(
['https://', req.get('Host'), req.url].join('')
);
}
next();
}
}
// Instruct the app
// to use the forceSSL
// middleware
app.use(forceSSL());
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
let port = process.env.PORT || 8081;
let server = app.listen(port, () => {
console.log('Server is now running.', port);
});
module.exports = server;