Authored by weiqingting

桩数据 路径

@@ -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){
@@ -6,7 +6,10 @@ @@ -6,7 +6,10 @@
6 method:"POST", 6 method:"POST",
7 params:[ 7 params:[
8 {name:"id",type:"Number"} 8 {name:"id",type:"Number"}
9 - ] 9 + ],
  10 + output:{
  11 + code:"我是桩数据"
  12 + }
10 } 13 }
11 } 14 }
12 } 15 }
@@ -6,7 +6,10 @@ @@ -6,7 +6,10 @@
6 method:"POST", 6 method:"POST",
7 params:[ 7 params:[
8 {name:"id",type:"Number"} 8 {name:"id",type:"Number"}
9 - ] 9 + ],
  10 + output:{
  11 + code:"我是桩数据"
  12 + }
10 } 13 }
11 } 14 }
12 } 15 }
@@ -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>
1 1
2 module.exports=function(Filter){ 2 module.exports=function(Filter){
3 - Filter.define("\/2","get",function(req,res,next){ 3 + Filter.define("\/3","get",function(req,res,next){
4 res.end("please login..."); 4 res.end("please login...");
5 // next(); 5 // next();
6 }); 6 });
  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");  
9 -  
10 -  
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;  
26 -  
27 -/*日志配置和Console*/  
28 -var Console=require("./libs/Console");  
29 -Console(apiCofig.log);  
30 -console.log("YOHO!");  
31 -  
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 -});  
38 -  
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 -});  
45 -  
46 -  
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 -});  
53 -  
54 -  
55 -/*路由控制*/  
56 -var KLH=function(obj,method,guid){  
57 - return function(req,res){  
58 - obj[method].call(obj,guid,req,res); 7 +var Scan=require("./Scan");
  8 +
  9 +
  10 +module.exports=function(apiCofig){
  11 +
  12 + // 记录下当前文档的路径
  13 + global.apps=apiCofig.apps;
  14 +
  15 + /*日志配置和Console*/
  16 + var Console=require("./Console");
  17 + Console(apiCofig.log);
  18 + console.log("YOHO!");
  19 +
  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 + });
  26 +
  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 + });
  33 +
  34 +
  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 + });
  41 +
  42 +
  43 + /*路由控制*/
  44 + var KLH=function(obj,method,guid){
  45 + return function(req,res){
  46 + obj[method].call(obj,guid,req,res);
  47 + }
59 } 48 }
60 -}  
61 -ContRegisters.routers.forEach(function(router){  
62 - var ROU=Router.route([router.url]);  
63 - var args=FilterRegisters.use(router.url,router.method).  
64 - concat(KLH(ContRegisters,"emit",router.guid));  
65 - ROU[router.method].apply(ROU,args);  
66 -}); 49 + ContRegisters.routers.forEach(function(router){
  50 + var ROU=Router.route([router.url]);
  51 + var args=FilterRegisters.use(router.url,router.method).
  52 + concat(KLH(ContRegisters,"emit",router.guid));
  53 + ROU[router.method].apply(ROU,args);
  54 + });
  55 +
  56 + App.use(apiCofig.uri, Router);
  57 +
67 58
68 -App.use('/', Router); 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);
69 64
  65 + /*加载中间间*/
  66 + apiCofig.use&&apiCofig.use(App);
70 67
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);  
76 68
  69 + var server = App.listen(apiCofig.port, function () {
  70 + var host = server.address().address;
  71 + var port = server.address().port;
  72 + });
77 73
78 -var server = App.listen(apiCofig.port, function () {  
79 - var host = server.address().address;  
80 - var port = server.address().port;  
81 -});  
  74 +}
@@ -7,10 +7,15 @@ @@ -7,10 +7,15 @@
7 } 7 }
8 */ 8 */
9 module.exports=function(logsConfig){ 9 module.exports=function(logsConfig){
10 - var Consoles=['log', 'info', 'warn', 'error', 'dir', 'assert'];  
11 - var log=console.log;  
12 -  
13 - 10 + var Consoles=['log', 'info', 'warn', 'error', 'dir', 'assert'];
  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)){
54 - funcs.push(function(callback){  
55 - __requestApi(me.config,me.apis[name],req,callback);  
56 - });  
57 names.push(name); 56 names.push(name);
  57 + if(me.config.mock){
  58 + funcs.push(me.apis[name].output);
  59 + }else{
  60 + funcs.push(function(callback){
  61 + __requestApi(me.config,me.apis[name],req,callback);
  62 + });
  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,
@@ -39,13 +39,15 @@ module.exports=function(url){ @@ -39,13 +39,15 @@ module.exports=function(url){
39 'folders': folderList 39 'folders': folderList
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){