pageModule.js
1.19 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 模块加载中间件
* @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;
}
console.log(res.appendData.module );
next();
}