Showing
10 changed files
with
109 additions
and
65 deletions
@@ -10,7 +10,7 @@ function ContModel(app){ | @@ -10,7 +10,7 @@ function ContModel(app){ | ||
10 | app.get("/1","module.index"); | 10 | app.get("/1","module.index"); |
11 | 11 | ||
12 | /*根据数据接口 渲染一个页面*/ | 12 | /*根据数据接口 渲染一个页面*/ |
13 | - app.get("/2","moduleA.index",["UserA-login"]); | 13 | + app.get("/2","module.index",["User-login"]); |
14 | 14 | ||
15 | /*适配器*/ | 15 | /*适配器*/ |
16 | app.get("/3","moduleA.index",["UserA-login"],function(key1){ | 16 | app.get("/3","moduleA.index",["UserA-login"],function(key1){ |
@@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
4 | <title></title> | 4 | <title></title> |
5 | </head> | 5 | </head> |
6 | <body> | 6 | <body> |
7 | -<h1>Views Index 文件 {{UserA-login.code}}</h1> | 7 | +<h1>Views Index 文件 {{User-login.code}}</h1> |
8 | <div>Views Index 文件 <strong>part</strong> {{>part}}</div> | 8 | <div>Views Index 文件 <strong>part</strong> {{>part}}</div> |
9 | <div>Views Index 文件 <strong>layout/A</strong> {{>layout/A}}</div> | 9 | <div>Views Index 文件 <strong>layout/A</strong> {{>layout/A}}</div> |
10 | <div>Views Index 文件 <strong>part</strong> {{>../../moduleA/views/index}}</div> | 10 | <div>Views Index 文件 <strong>part</strong> {{>../../moduleA/views/index}}</div> |
index.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +/*下载最新node v5.10.1*/ | ||
4 | +var path = require('path'); | ||
5 | + | ||
6 | +/*获取配置接口信息*/ | ||
7 | +var apiCofig={ | ||
8 | + root:__dirname, | ||
9 | + apps:path.join(__dirname,"apps"), | ||
10 | + domain:"http://192.168.102.202:8088/platform", | ||
11 | + port:3000, | ||
12 | + log:{ | ||
13 | + consoles:["log"], | ||
14 | + src:"" | ||
15 | + }, | ||
16 | + mock:true, | ||
17 | + uri:'/', | ||
18 | + MVC:{//__dirname+"/apps/{.*}/interfaces/{.*}.js" | ||
19 | + Interfacer:path.join(__dirname,"apps/0/interfaces/1.js").replace(/0|1/g,'{.*}'), | ||
20 | + Controller:path.join(__dirname,"apps/0/controllers/1.js").replace(/0|1/g,'{.*}'), | ||
21 | + filters:path.join(__dirname,"filters.js") | ||
22 | + }, | ||
23 | + use:function(app){ | ||
24 | + | ||
25 | + } | ||
26 | +} | ||
27 | +module.exports = require('./libs/App')(apiCofig); |
1 | -/*下载最新node v5.10.1*/ | ||
2 | var path = require('path'); | 1 | var path = require('path'); |
3 | 2 | ||
4 | var express = require('express'); | 3 | var express = require('express'); |
5 | var App = express(); | 4 | var App = express(); |
6 | var Router = express.Router(); | 5 | var Router = express.Router(); |
7 | 6 | ||
8 | -var Scan=require("./libs/Scan"); | 7 | +var Scan=require("./Scan"); |
9 | 8 | ||
10 | 9 | ||
10 | +module.exports=function(apiCofig){ | ||
11 | 11 | ||
12 | -/*获取配置接口信息*/ | ||
13 | -var apiCofig={ | ||
14 | - root:__dirname, | ||
15 | - apps:path.join(__dirname,"apps"), | ||
16 | - domain:"http://192.168.102.202:8088/platform", | ||
17 | - port:3000, | ||
18 | - log:{ | ||
19 | - consoles:["log"] | ||
20 | - }, | ||
21 | - mock:true | ||
22 | -} | ||
23 | - | ||
24 | -// 记录下当前文档的路径 | ||
25 | -global.apps=apiCofig.apps; | 12 | + // 记录下当前文档的路径 |
13 | + global.apps=apiCofig.apps; | ||
26 | 14 | ||
27 | -/*日志配置和Console*/ | ||
28 | -var Console=require("./libs/Console"); | ||
29 | -Console(apiCofig.log); | ||
30 | -console.log("YOHO!"); | 15 | + /*日志配置和Console*/ |
16 | + var Console=require("./Console"); | ||
17 | + Console(apiCofig.log); | ||
18 | + console.log("YOHO!"); | ||
31 | 19 | ||
32 | -/*接口层*/ | ||
33 | -var Interfacer=require("./libs/Interfacer"); | ||
34 | -var InterRegisters= new Interfacer(apiCofig); | ||
35 | -Scan("./apps/{.*}/interfaces/{.*}.js").forEach(function(src){ | ||
36 | - InterRegisters.register(require(".\/"+src)); | ||
37 | -}); | 20 | + /*接口层*/ |
21 | + var Interfacer=require("./Interfacer"); | ||
22 | + var InterRegisters= new Interfacer(apiCofig); | ||
23 | + Scan(apiCofig.MVC.Interfacer).forEach(function(src){ | ||
24 | + InterRegisters.register(require(src)); | ||
25 | + }); | ||
38 | 26 | ||
39 | -/*过滤器*/ | ||
40 | -var Filter=require("./libs/Filter"); | ||
41 | -var FilterRegisters= new Filter(InterRegisters); | ||
42 | -Scan("./filters.js").forEach(function(src){ | ||
43 | - require(".\/"+src)(FilterRegisters); | ||
44 | -}); | 27 | + /*过滤器*/ |
28 | + var Filter=require("./Filter"); | ||
29 | + var FilterRegisters= new Filter(InterRegisters); | ||
30 | + Scan(apiCofig.MVC.filters).forEach(function(src){ | ||
31 | + require(src)(FilterRegisters); | ||
32 | + }); | ||
45 | 33 | ||
46 | 34 | ||
47 | -/*控制器层*/ | ||
48 | -var Controller=require("./libs/Controller"); | ||
49 | -var ContRegisters=new Controller(InterRegisters); | ||
50 | -Scan("./apps/{.*}/controllers/{.*}.js").forEach(function(src){ | ||
51 | - require(".\/"+src)(ContRegisters); | ||
52 | -}); | 35 | + /*控制器层*/ |
36 | + var Controller=require("./Controller"); | ||
37 | + var ContRegisters=new Controller(InterRegisters); | ||
38 | + Scan(apiCofig.MVC.Controller).forEach(function(src){ | ||
39 | + require(src)(ContRegisters); | ||
40 | + }); | ||
53 | 41 | ||
54 | 42 | ||
55 | -/*路由控制*/ | ||
56 | -var KLH=function(obj,method,guid){ | 43 | + /*路由控制*/ |
44 | + var KLH=function(obj,method,guid){ | ||
57 | return function(req,res){ | 45 | return function(req,res){ |
58 | obj[method].call(obj,guid,req,res); | 46 | obj[method].call(obj,guid,req,res); |
59 | } | 47 | } |
60 | -} | ||
61 | -ContRegisters.routers.forEach(function(router){ | 48 | + } |
49 | + ContRegisters.routers.forEach(function(router){ | ||
62 | var ROU=Router.route([router.url]); | 50 | var ROU=Router.route([router.url]); |
63 | var args=FilterRegisters.use(router.url,router.method). | 51 | var args=FilterRegisters.use(router.url,router.method). |
64 | concat(KLH(ContRegisters,"emit",router.guid)); | 52 | concat(KLH(ContRegisters,"emit",router.guid)); |
65 | ROU[router.method].apply(ROU,args); | 53 | ROU[router.method].apply(ROU,args); |
66 | -}); | 54 | + }); |
67 | 55 | ||
68 | -App.use('/', Router); | 56 | + App.use(apiCofig.uri, Router); |
69 | 57 | ||
70 | 58 | ||
71 | -/*View 设置*/ | ||
72 | -var Viewer=require("./libs/Viewer"); | ||
73 | -var Viewer=new Viewer(App,require("handlebars")); | ||
74 | -App.set('view engine', 'html'); | ||
75 | -App.engine('html', Viewer.engine); | 59 | + /*View 设置*/ |
60 | + var Viewer=require("./Viewer"); | ||
61 | + var Viewer=new Viewer(App,require("handlebars")); | ||
62 | + App.set('view engine', 'html'); | ||
63 | + App.engine('html', Viewer.engine); | ||
76 | 64 | ||
65 | + /*加载中间间*/ | ||
66 | + apiCofig.use&&apiCofig.use(App); | ||
77 | 67 | ||
78 | -var server = App.listen(apiCofig.port, function () { | 68 | + |
69 | + var server = App.listen(apiCofig.port, function () { | ||
79 | var host = server.address().address; | 70 | var host = server.address().address; |
80 | var port = server.address().port; | 71 | var port = server.address().port; |
81 | -}); | ||
72 | + }); | ||
73 | + | ||
74 | +} |
@@ -8,9 +8,14 @@ | @@ -8,9 +8,14 @@ | ||
8 | */ | 8 | */ |
9 | module.exports=function(logsConfig){ | 9 | module.exports=function(logsConfig){ |
10 | var Consoles=['log', 'info', 'warn', 'error', 'dir', 'assert']; | 10 | var Consoles=['log', 'info', 'warn', 'error', 'dir', 'assert']; |
11 | - var log=console.log; | ||
12 | - | ||
13 | - | 11 | + var log=console.log, |
12 | + info=console.info, | ||
13 | + warn=console.warn, | ||
14 | + error=console.error, | ||
15 | + dir=console.dir, | ||
16 | + assert=console.assert; | ||
17 | + | ||
18 | + /*重写console方法*/ | ||
14 | console.log = function(){ | 19 | console.log = function(){ |
15 | var args=[].slice.call(arguments, 0); | 20 | var args=[].slice.call(arguments, 0); |
16 | args[0]="log:"+args[0]; | 21 | args[0]="log:"+args[0]; |
@@ -19,7 +24,7 @@ module.exports=function(logsConfig){ | @@ -19,7 +24,7 @@ module.exports=function(logsConfig){ | ||
19 | console.error = function(){ | 24 | console.error = function(){ |
20 | var args=[].slice.call(arguments, 0); | 25 | var args=[].slice.call(arguments, 0); |
21 | args[0]="error:"+args[0]; | 26 | args[0]="error:"+args[0]; |
22 | - log.apply(console, args); | 27 | + error.apply(console, args); |
23 | }; | 28 | }; |
24 | 29 | ||
25 | 30 | ||
@@ -33,10 +38,9 @@ module.exports=function(logsConfig){ | @@ -33,10 +38,9 @@ module.exports=function(logsConfig){ | ||
33 | return !(array2.indexOf(item)>-1); | 38 | return !(array2.indexOf(item)>-1); |
34 | }); | 39 | }); |
35 | } | 40 | } |
36 | - | 41 | + logsConfig.consoles=logsConfig.consoles||[]; |
37 | if(logsConfig.consoles){ | 42 | if(logsConfig.consoles){ |
38 | var invalidCons=difference(Consoles,logsConfig.consoles); | 43 | var invalidCons=difference(Consoles,logsConfig.consoles); |
39 | - console.log(invalidCons); | ||
40 | invalidCons.forEach(function (f) { | 44 | invalidCons.forEach(function (f) { |
41 | console[f] = function () { | 45 | console[f] = function () { |
42 | }; | 46 | }; |
@@ -49,17 +49,29 @@ function __requestApi(config,apiOpt,req,callback){ | @@ -49,17 +49,29 @@ function __requestApi(config,apiOpt,req,callback){ | ||
49 | */ | 49 | */ |
50 | Interfacer.prototype.require=function(mos,req,res,cb){ | 50 | Interfacer.prototype.require=function(mos,req,res,cb){ |
51 | var me=this,funcs=[],names=[]; | 51 | var me=this,funcs=[],names=[]; |
52 | + | ||
52 | mos.forEach(function(name){ | 53 | mos.forEach(function(name){ |
54 | + | ||
53 | if(me.apis.hasOwnProperty(name)){ | 55 | if(me.apis.hasOwnProperty(name)){ |
56 | + names.push(name); | ||
57 | + if(me.config.mock){ | ||
58 | + funcs.push(me.apis[name].output); | ||
59 | + }else{ | ||
54 | funcs.push(function(callback){ | 60 | funcs.push(function(callback){ |
55 | __requestApi(me.config,me.apis[name],req,callback); | 61 | __requestApi(me.config,me.apis[name],req,callback); |
56 | }); | 62 | }); |
57 | - names.push(name); | 63 | + } |
58 | } | 64 | } |
59 | }); | 65 | }); |
66 | + | ||
60 | if(funcs.length!=mos.length){ | 67 | if(funcs.length!=mos.length){ |
61 | return {err:"某个key 可能不存在!"}; | 68 | return {err:"某个key 可能不存在!"}; |
62 | } | 69 | } |
70 | + if(me.config.mock){ | ||
71 | + console.log(funcs); | ||
72 | + cb(null,funcs,names); | ||
73 | + return; | ||
74 | + } | ||
63 | async.parallel(funcs, function(err, results){ | 75 | async.parallel(funcs, function(err, results){ |
64 | if(err){ | 76 | if(err){ |
65 | return cb(err,null,names); | 77 | return cb(err,null,names); |
@@ -3,7 +3,7 @@ var path = require('path'); | @@ -3,7 +3,7 @@ var path = require('path'); | ||
3 | 3 | ||
4 | module.exports=function(url){ | 4 | module.exports=function(url){ |
5 | var tokens=[],files=[],dir=""; | 5 | var tokens=[],files=[],dir=""; |
6 | - url.split(/\/\{|\}/).forEach(function(item,index) { | 6 | + url.split(/\{|\}/).forEach(function(item,index) { |
7 | if(item){ | 7 | if(item){ |
8 | tokens.push({ | 8 | tokens.push({ |
9 | value:item, | 9 | value:item, |
@@ -40,12 +40,14 @@ module.exports=function(url){ | @@ -40,12 +40,14 @@ module.exports=function(url){ | ||
40 | } | 40 | } |
41 | } | 41 | } |
42 | 42 | ||
43 | + | ||
43 | if(fs.statSync(tokens[0].value).isFile()){ | 44 | if(fs.statSync(tokens[0].value).isFile()){ |
44 | return [path.normalize(tokens[0].value)]; | 45 | return [path.normalize(tokens[0].value)]; |
45 | } | 46 | } |
46 | 47 | ||
47 | - url=url.replace(/^\W+/g,'').replace(/\//g,'\\\\').replace(/\{/g,'(').replace(/\}/g,'?)'); | 48 | + url=url.replace(/^\W+/g,'').replace(/\\/g,'\\\\').replace(/\{/g,'(').replace(/\}/g,'?)'); |
48 | var regex = new RegExp("^"+url+"$"); | 49 | var regex = new RegExp("^"+url+"$"); |
50 | + | ||
49 | var obj=scanFolder(tokens[0].value),result=[]; | 51 | var obj=scanFolder(tokens[0].value),result=[]; |
50 | if(path.extname(url)){ | 52 | if(path.extname(url)){ |
51 | result=obj.files.filter(function(item){ | 53 | result=obj.files.filter(function(item){ |
-
Please register or login to post a comment