all files / direwolf/lib/core/ controller.js

100% Statements 12/12
100% Branches 0/0
100% Functions 0/0
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                                 
/**
 * Created by zdy on 2016/10/8.
 */
 
'use strict';
 
const Path = require('path');
const fs = require('fs');
 
module.exports = app => {
    // 应用中控制器目录
    const controllerDir = Path.join(app.config.baseDir, app.config._direwolfConfig.controllerDir);
 
    // 获取目录中的文件名
    const controllerFiles = fs.readdirSync(controllerDir);
 
    const controller = {};
 
    for (const fileName of controllerFiles) {
        const objName = Path.basename(fileName, '.js');
        const _path = Path.join(controllerDir, objName);
 
        // 一级对象为控制器中的文件名,二级对象为控制器中exports出来的方法
        controller[objName] = require(_path);
    }
 
    app.controller = controller;
    return controller;
};