-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·61 lines (51 loc) · 1.54 KB
/
index.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
#!/usr/bin/env node
var newrelic = require('newrelic');
var express = require('express');
var compression = require('compression');
var seo = require('mean-seo');
var routes = require('./app/js/lib/routes');
var posts = require('./app/js/lib/posts-json');
var _ = require('lodash');
var validRoutes = [];
function normalizeRoute(route) {
return route.replace(/^\//, '').replace(/\/$/, '');
}
_.each(routes, function (route, path) {
if (route.posts) {
_.each(posts, function (post) {
validRoutes.push(normalizeRoute('post/' + post.slug));
});
} else {
validRoutes.push(normalizeRoute(path));
}
});
var app = express();
// Enable gzip compression.
app.use(compression());
// Enable PhantomJS SEO.
if (process.env.REDISCLOUD_URL) {
// If we've got Redis available, use that.
app.use(seo({ cacheClient: 'rediscloud' }));
} else {
// Otherwise, use regular disk-based cache.
app.use(seo());
}
app.use(express.static(__dirname + '/dist'));
app.get('/sitemap.xml', function(request, response) {
response.sendFile(__dirname + '/dist/sitemap.xml');
});
app.get('*', function(request, response) {
// var path = request.url.replace(/\?.*$/, '').replace(/\/$/, '').replace(/^\//, '');
// var found = false;
// for (var i = 0, n = validRoutes.length; i < n; i++) {
// if (validRoutes[i] === path) {
// found = true;
// break;
// }
// }
response.sendFile(__dirname + '/dist/index.html');
});
var port = process.env.PORT || 8080;
app.listen(port, function () {
console.log('Listening on port: ' + port + '.');
});