var fs = require('fs'),
  path = require('path'),
  express = require('express');

var rroute = function (dir) {
  var app = express();
  if(fs.existsSync(dir)){
    fs.readdirSync(dir)
      .forEach(function(rel){
        var abs = path.join(dir, rel);
        var stats = fs.statSync(abs);
        if(stats.isDirectory()){
          app.use('/'+rel,rroute(abs));
        }else if(rel.indexOf('.js') === (rel.length - 3)){
          var clean = rel.substr(0,rel.length-3);
          if(clean === 'index'){
            clean = '/';
          }else{
            clean = '/'+clean+'/';
          }
          app.use(clean, require(abs));
        }
      });
  }
  return app;
};

module.exports = rroute;