From cfeff0b7ce3189f6d4b4ba8e810e535aa0c2f636 Mon Sep 17 00:00:00 2001 From: Ali Movahedi Date: Tue, 16 Sep 2014 10:18:54 +0430 Subject: [PATCH] Add counter --- ipsum.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/ipsum.js b/ipsum.js index f00c8e7..5b1f804 100644 --- a/ipsum.js +++ b/ipsum.js @@ -1,7 +1,12 @@ var express = require('express'), - app = express(); + app = express(), + server = require('http').Server(app), + io = require('socket.io')(server), + fs = require('fs'); -app.use(express.static(__dirname + '/ipsum', {maxAge: 60*60*24*7})); +var count = require('./count.json'); + +app.use(express.static(__dirname + '/client', {maxAge: 60*60*24*7})); module.exports.app = app; @@ -87,6 +92,19 @@ function loremipsum(data) { } } function go(url) { - var req = url.split('/'); - return loremipsum(req.slice(1)); + var req = url.split('/').slice(1); + counting(loremipsum(req)); + return loremipsum(req); +} + +//COUNTER +io.on('connection', function(socket) { + io.emit('counter', count); +}); +function counting(data) { + var r = data.split(' ').join('').length + count; + fs.writeFile(__dirname + '/count.json', r, function() { + count = +(fs.readFileSync(__dirname + '/count.json', 'utf8')); + }) + io.emit('counter', r); }