pageModule.js 1.14 KB
/**
 * 模块加载中间件
 * @author h1bomb
 */

//模块环境适配
var common = require('../util/common');
var path = require('path');
var url = require('url');

var $routers = { GET: {}, POST: {}, ALL: [] };
common.readDir(path.join(__dirname, '../interface'), function (module) {
    Array.prototype.push.apply($routers.ALL, module.res);
});
//刷选get/post请求
for (var i in $routers.ALL) {
    var router = $routers.ALL[i];
    var methord = router.method.toUpperCase();
    var url = router.route;
    $routers[methord || "GET"][url] = router;
}

/**
 * 模块加载中间件
 * @param  {Object} req  请求对象
 * @param  {Object} res  响应对象
 * @param  {Function} next 执行下个中间件
 * @return {void}       无返回
 */
module.exports = function (req, res, next) {
    var path = 'main1';
    if (!req.xhr && req.route && req.route.methods.get) {
        path = $routers.GET[req.route.path].src|| path;
    }
    var module = common.moduleProc(path);
    //判断是否已存在appendData
    if (!res.appendData) {
        res.appendData = { module: module };
    } else {
        res.appendData.module = module;
    }
    
    next();
}