2015-10-20 12:54:48 -04:00
|
|
|
if (typeof process.env.NEW_RELIC_LICENSE_KEY === 'string') {
|
|
|
|
require('newrelic');
|
|
|
|
}
|
|
|
|
|
2015-09-04 01:26:56 -04:00
|
|
|
var compression = require('compression');
|
|
|
|
var express = require('express');
|
2015-10-20 12:48:32 -04:00
|
|
|
var path = require('path');
|
2015-09-30 11:04:51 -04:00
|
|
|
var proxy = require('express-http-proxy');
|
2015-10-20 12:48:32 -04:00
|
|
|
var url = require('url');
|
2015-09-04 01:26:56 -04:00
|
|
|
|
|
|
|
var handler = require('./handler');
|
|
|
|
var log = require('./log');
|
|
|
|
var routes = require('./routes.json');
|
|
|
|
|
2015-11-10 13:38:28 -05:00
|
|
|
var isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
2015-10-29 12:06:13 -04:00
|
|
|
// Create server
|
2015-09-04 01:26:56 -04:00
|
|
|
var app = express();
|
|
|
|
app.disable('x-powered-by');
|
2015-10-29 12:06:13 -04:00
|
|
|
|
|
|
|
// Block POST & PUT requests in production
|
2015-11-10 13:38:28 -05:00
|
|
|
if (isProduction) {
|
2015-10-29 12:06:13 -04:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
if (req.method === 'GET') return next();
|
|
|
|
if (req.method === 'OPTIONS') return next();
|
|
|
|
if (req.method === 'HEAD') return next();
|
|
|
|
|
|
|
|
res.writeHead(405, {'content-type' : 'application/json'});
|
|
|
|
res.end('{"error": "Method not allowed"}');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server setup
|
2015-09-04 01:26:56 -04:00
|
|
|
app.use(log());
|
|
|
|
app.use(compression());
|
2015-11-10 13:39:10 -05:00
|
|
|
if (isProduction) {
|
|
|
|
app.use(express.static(path.resolve(__dirname, '../build'), {
|
2016-01-06 14:25:27 -05:00
|
|
|
etag: 'strong',
|
2015-11-10 13:39:10 -05:00
|
|
|
maxAge: '1y'
|
|
|
|
}));
|
|
|
|
}
|
2015-10-20 12:48:32 -04:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
req._path = url.parse(req.url).path;
|
|
|
|
next();
|
|
|
|
});
|
2015-09-04 01:26:56 -04:00
|
|
|
|
|
|
|
// Bind routes
|
2015-10-01 09:50:00 -04:00
|
|
|
for (var routeId in routes) {
|
|
|
|
var route = routes[routeId];
|
2015-10-20 12:48:32 -04:00
|
|
|
app.get(route.pattern, handler(route));
|
2015-09-04 01:26:56 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:00:19 -05:00
|
|
|
if (typeof process.env.NODE_SENTRY_DSN === 'string') {
|
2015-10-22 15:05:55 -04:00
|
|
|
var raven = require('raven');
|
2015-10-23 10:43:30 -04:00
|
|
|
app.get('/sentrythrow', function mainHandler () { throw new Error('Sentry Test'); });
|
2015-10-22 15:05:55 -04:00
|
|
|
|
2015-10-23 10:43:30 -04:00
|
|
|
// These handlers must be applied _AFTER_ other routes have been applied
|
2015-11-12 17:00:19 -05:00
|
|
|
app.use(raven.middleware.express.requestHandler(process.env.NODE_SENTRY_DSN));
|
|
|
|
app.use(raven.middleware.express.errorHandler(process.env.NODE_SENTRY_DSN));
|
2015-10-23 10:43:30 -04:00
|
|
|
app.use(function errorHandler (err, req, res, next) {
|
|
|
|
res.append('X-Sentry-ID:' + res.sentry);
|
2015-10-22 15:12:38 -04:00
|
|
|
res.status(500);
|
|
|
|
next(err);
|
2015-10-23 10:43:30 -04:00
|
|
|
});
|
2015-10-22 15:05:55 -04:00
|
|
|
|
2015-11-12 17:00:19 -05:00
|
|
|
raven.patchGlobal(process.env.NODE_SENTRY_DSN, function () {
|
2015-10-23 10:43:30 -04:00
|
|
|
process.exit(-1);
|
|
|
|
});
|
2015-10-22 15:05:55 -04:00
|
|
|
}
|
|
|
|
|
2015-11-10 13:38:28 -05:00
|
|
|
if (!isProduction) {
|
2015-11-10 13:39:10 -05:00
|
|
|
// Use webpack-dev-server in development
|
2016-03-31 12:24:33 -04:00
|
|
|
var webpackDevMiddleware = require('webpack-dev-middleware');
|
|
|
|
var webpack = require('webpack');
|
2015-11-10 13:39:10 -05:00
|
|
|
var compiler = webpack(require('../webpack.config.js'));
|
|
|
|
app.use(webpackDevMiddleware(compiler, {
|
|
|
|
headers: {
|
|
|
|
'X-From-Webpack': true
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2015-12-04 09:52:10 -05:00
|
|
|
var proxyHost = process.env.FALLBACK || '';
|
2015-12-03 15:53:51 -05:00
|
|
|
if (proxyHost !== '') {
|
2015-12-03 10:41:02 -05:00
|
|
|
// Fall back to scratchr2 in development
|
|
|
|
// This proxy middleware must come last
|
|
|
|
app.use('/', proxy(proxyHost));
|
|
|
|
}
|
2015-09-30 11:04:51 -04:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:26:56 -04:00
|
|
|
// Start listening
|
2015-09-16 10:40:03 -04:00
|
|
|
var port = process.env.PORT || 8333;
|
2015-09-04 01:26:56 -04:00
|
|
|
app.listen(port, function () {
|
|
|
|
process.stdout.write('Server listening on port ' + port + '\n');
|
2015-10-01 09:50:00 -04:00
|
|
|
if (proxyHost) {
|
|
|
|
process.stdout.write('Proxy host: ' + proxyHost + '\n');
|
|
|
|
}
|
2015-09-04 01:26:56 -04:00
|
|
|
});
|