From ffcb50e196b43d95dd65aa0eeb16f5b5f58ffde8 Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 10 Jun 2015 05:04:17 +0300 Subject: [PATCH 1/2] removed express from app.js --- app.js | 41 +---------------------------------------- lib/express.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 lib/express.js diff --git a/app.js b/app.js index 3f87862..38f3b01 100644 --- a/app.js +++ b/app.js @@ -18,46 +18,7 @@ var env = 'production'; if( process.env.NODE_ENV !== 'production' ) { - var express = require('express'); - var app = express(); - var path = require('path'); - var bodyParser = require('body-parser'); - - // view engine setup - app.set('views', path.join(__dirname, 'src/views')); - app.set('view engine', 'jade'); - app.use(bodyParser.json()); - app.use(bodyParser.urlencoded({ extended: false })); - app.use(express.static(path.join(__dirname, 'dist'))); - - app.get('/', function(req, res) { - res.render('index'); - }); - - // catch 404 and forward to error handler - app.use(function(req, res, next) { - var err = new Error('Not Found'); - err.status = 404; - next(err); - }); - - // error handlers - app.use(function(err, req, res, next) { - res.status(err.status || 500); - res.render('error', { - message: err.message, - error: err - }); - }); - - // production error handler - app.use(function(err, req, res, next) { - res.status(err.status || 500); - res.render('error', { - message: err.message, - error: {} - }); - }); + var app = require('./lib/express'); server = require('http').createServer(app); } diff --git a/lib/express.js b/lib/express.js new file mode 100644 index 0000000..747fe4e --- /dev/null +++ b/lib/express.js @@ -0,0 +1,42 @@ +var express = require('express'); +var app = express(); +var path = require('path'); +var bodyParser = require('body-parser'); + +// view engine setup +app.set('views', path.join(__dirname, '../src/views')); +app.set('view engine', 'jade'); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); +app.use(express.static(path.join(__dirname, '../dist'))); + +app.get('/', function(req, res) { + res.render('index'); +}); + +// catch 404 and forward to error handler +app.use(function(req, res, next) { + var err = new Error('Not Found'); + err.status = 404; + next(err); +}); + +// error handlers +app.use(function(err, req, res, next) { + res.status(err.status || 500); + res.render('error', { + message: err.message, + error: err + }); +}); + +// production error handler +app.use(function(err, req, res, next) { + res.status(err.status || 500); + res.render('error', { + message: err.message, + error: {} + }); +}); + +module.exports = app; \ No newline at end of file From a50a3b29f9e5b56a45666148f885483648fd6b66 Mon Sep 17 00:00:00 2001 From: cubedro Date: Wed, 10 Jun 2015 05:32:25 +0300 Subject: [PATCH 2/2] app.js refactoring --- app.js | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index 38f3b01..a26e09b 100644 --- a/app.js +++ b/app.js @@ -3,30 +3,27 @@ var logger = require('./lib/utils/logger'); var chalk = require('chalk'); var askedForHistoryTime = 0; - -var Primus = require('primus'), - api, - client; - var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; -var Collection = require('./lib/collection'); -var Nodes = new Collection(); - -var server; -var env = 'production'; +// Init http server if( process.env.NODE_ENV !== 'production' ) { var app = require('./lib/express'); - server = require('http').createServer(app); } else -{ server = require('http').createServer(); -} + +// Init socket vars +var Primus = require('primus'); +var api; +var client; +var server; + + +// Init API Socket connection api = new Primus(server, { transformer: 'websockets', pathname: '/api', @@ -36,13 +33,20 @@ api = new Primus(server, { api.use('emit', require('primus-emit')); api.use('spark-latency', require('primus-spark-latency')); -var client = new Primus(server, { + +// Init Client Socket connection +client = new Primus(server, { transformer: 'websockets', pathname: '/primus', parser: 'JSON' }); -var clientLatency = 0; +client.use('emit', require('primus-emit')); + + +// Init collections +var Collection = require('./lib/collection'); +var Nodes = new Collection(); Nodes.setChartsCallback(function (err, charts) { @@ -59,8 +63,8 @@ Nodes.setChartsCallback(function (err, charts) } }); -client.use('emit', require('primus-emit')); +// Init API Socket events api.on('connection', function (spark) { console.info('API', 'CON', 'Open:', spark.address.ip); @@ -343,8 +347,7 @@ client.on('connection', function (clientSpark) clientSpark.on('client-pong', function (data) { - var start = (!_.isUndefined(data) && !_.isUndefined(data.serverTime) ? data.serverTime : clientLatency); - var latency = Math.ceil( (_.now() - start) / 2 ); + var latency = Math.ceil( (_.now() - data.serverTime) / 2 ); clientSpark.emit('client-latency', { latency: latency }); }); @@ -352,12 +355,10 @@ client.on('connection', function (clientSpark) var latencyTimeout = setInterval( function () { - clientLatency = _.now(); - client.write({ action: 'client-ping', data: { - serverTime: clientLatency + serverTime: _.now() } }); }, 5000);