This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
forked from mozilla/games.mozilla.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (54 loc) · 1.93 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
66
67
68
69
const fs = require('fs');
const path = require('path');
const electricity = require('electricity');
const express = require('express');
const internalIp = require('internal-ip');
const yonder = require('yonder');
let app = express();
const IS_DEV = app.get('env') === 'development';
const CACHE_MAX_AGE = IS_DEV ? -1 : 60;
const CACHE_EXPIRES = IS_DEV ? 0 : 60;
const PORT_SERVER = parseInt(process.env.PORT || process.env.PORT || '8080');
const PUBLIC_DIR = path.join(__dirname, '_build');
const ROUTER_PATH = path.join(PUBLIC_DIR, 'ROUTER');
app.initServer = function () {
// Serve static files (very similar to how Surge and GitHub Pages do).
// See http://expressjs.com/en/starter/static-files.html for usage.
let electricityOptions = {
'hashify': false,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
}
};
if (CACHE_MAX_AGE) {
electricityOptions.headers['Cache-Control'] = 'max-age=' + CACHE_MAX_AGE;
}
if (CACHE_EXPIRES) {
electricityOptions.headers['Expires'] = CACHE_EXPIRES;
}
var serveStatic = electricity.static(PUBLIC_DIR, electricityOptions);
app.use(serveStatic);
// Create server-side redirects (defined in the `ROUTER` file).
// See https://github.com/sintaxi/yonder#readme for usage.
if (fs.existsSync(ROUTER_PATH)) {
app.use(yonder.middleware(ROUTER_PATH));
}
app.use(function (req, res, next) {
res.status(404);
if (req.accepts('html')) {
res.sendFile('404.html', {root: PUBLIC_DIR});
return;
}
res.type('txt').send('Not found');
});
if (!module.parent) {
let listener = app.listen(PORT_SERVER, function () {
console.log('Listening on port http://%s:%s', internalIp.v4(), listener.address().port);
});
}
return app;
};
app.initServer();
module.exports = app;