'use strict';

/* TODO
 *   write log module to write and print
 *   add countdown timer
 */
var http    = require('http')
  , fs      = require('fs')
  , log     = console.log
  , PORT    = 6060
  , express = require('express')
  , app     = express()
  , util    = require('util');

/*
 * use ejs to serve static files
 */
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.static(__dirname + '/'));

/*
 * HTTP request routers
 */
app.get('/', function (req, res, next) {
    var info = util.format('[%s]: %s,\t%s,\t%s',
      new Date(), req.method, req.ip, req.path);
    info += '\n';
    fs.appendFile('logs/connect.log', info, function (error) {
        if (error) throw new Error(("Error writing to file: " + error));
    });
    next();
}, function (req, res, next) {
	res.render('index.ejs');
});

/*
 * listen on port 6060 (rerouted from 80)
 */
app.listen(PORT, function () {
	console.log('facer listening on port', PORT);
});

/*
 * restart server on file change
 */
var forever = require('forever-monitor');
var child = new (forever.Monitor)('facer.js', {
	max: 3,
	silent: true,
	args: []
});
child.on('exit', function () {
	log('server.js has exited after 3 restart');
}).on('watch:restart', function (info) {
	log('restarting server because "%s" changed', info.file);
}).on('restart', function () {
	log('restarting server for %s time', child.times);
});


@@ -508,24 +508,284 @@
 es, next) %7B%0A
+    logConnection(req);%0A    next();%0A%7D, function (req, res, next) %7B%0A%09res.render('index.ejs');%0A%7D);%0A%0A/*%0A * listen on port 6060 (rerouted from 80)%0A */%0Aapp.listen(PORT, function () %7B%0A%09console.log('facer listening on port', PORT);%0A%7D);%0A%0Afunction logConnection(req) %7B%0A
     var info
@@ -1036,212 +1036,9 @@
 %7D);%0A
-    next();%0A%7D, function (req, res, next) %7B%0A%09res.render('index.ejs');%0A%7D);%0A%0A/*%0A * listen on port 6060 (rerouted from 80)%0A */%0Aapp.listen(PORT, function () %7B%0A%09console.log('facer listening on port', PORT);%0A%7D);
+%7D
 %0A%0A/*

Move connection logging operation to separate function