-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (53 loc) · 1.54 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
var broccoli = require('broccoli');
var copyDereferenceSync = require('copy-dereference').sync;
var express = require('express');
var fs = require('fs');
var gzipStatic = require('connect-gzip-static');
var http = require('http');
var path = require('path');
var Q = require('q');
var rimraf = require('rimraf');
// Clean existing data
function clean(directory) {
if (fs.existsSync(directory)) {
rimraf.sync(directory);
}
}
// Build application
function build(output) {
var node = broccoli.loadBrocfile();
var builder = new broccoli.Builder(node);
return builder.build()
.then(function (hash) {
var dir = hash.directory;
copyDereferenceSync(dir, output);
})
.finally(function () {
return builder.cleanup();
});
}
// Start up server
function serve() {
var app = express();
var router = express.Router();
router.get('/*', function(req, res, next) {
// Check for resource requests and redirect to the index file for them
if (path.extname(req.path).length > 0) {
next();
} else {
req.url = "/index.html";
next();
}
});
app.use(router);
app.use(gzipStatic(__dirname + "/public", {
maxAge: 31536000 * 1000 // 1 year
}));
var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
}
Q(serve())
.done();