app.js 1.18 KB
/**
 * 展示服务
 */
var express = require('express');
var log4js = require('log4js');
var path = require('path');

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('web.log'), 'weblog');

var weblogger = log4js.getLogger('weblog');
weblogger.setLevel('INFO');

var log = log4js.connectLogger(weblogger);

var app = express();

app.use(log);
app.use(error);
app.use(express.static(path.join(__dirname, '../tests')));

app.map = function(a, route)
{
  route = route || '';
  for (var key in a)
  {
    switch (typeof a[key])
    {
      // { '/path': { ... }}
      case 'object':
        app.map(a[key], route + key);
        break;
      // get: function(){ ... }
      case 'function':
        app[key](route, a[key]);
        break;
    }
  }
};

function gif(req, res)
{
   var gif = "R0lGODlhAQABAID/AP///wAAACwAAAAAAQABAAACAkQBADs=";
   var dataBuffer = new Buffer(gif, 'base64');
   res.send(dataBuffer);
}

app.map(
{
  '/ya.gif':
  {
    get : gif
  }
});


function error(err, req, res, next)
{
  syslog.error("error:"+err);
  res.status(500);
  res.send('Internal Server Error');
}

app.listen(8081, function() {
    console.log('testserver at http://localhost:8081/');
});