App.js
2.09 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var path = require('path');
var express = require('express');
var App = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var Router = express.Router();
var Scan=require("./Scan");
module.exports=function(apiCofig){
// 记录下当前文档的路径
global.apps=apiCofig.apps;
/*日志配置和Console*/
var Console=require("./Console");
Console(apiCofig.log);
console.log("YOHO!");
/*接口层*/
var Interfacer=require("./Interfacer");
var InterRegisters= new Interfacer(apiCofig);
apiCofig.MVC.Interfacer&&
Scan(apiCofig.MVC.Interfacer).forEach(function(src){
InterRegisters.register(require(src));
});
/*过滤器*/
var Filter=require("./Filter");
var FilterRegisters= new Filter(InterRegisters);
apiCofig.MVC.filters&&
Scan(apiCofig.MVC.filters).forEach(function(src){
require(src)(FilterRegisters);
});
/*控制器层*/
var Controller=require("./Controller");
var ContRegisters=new Controller(InterRegisters);
apiCofig.MVC.Controller&&
Scan(apiCofig.MVC.Controller).forEach(function(src){
require(src)(ContRegisters);
});
/*内置中间件 对Http请求解析*/
App.use(bodyParser.json()); // for parsing application/json
App.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// App.use(multer()); // for parsing multipart/form-data
/*路由控制*/
var KLH=function(obj,method,guid){
return function(req,res){
obj[method].call(obj,guid,req,res);
}
}
ContRegisters.routers.forEach(function(router){
var ROU=Router.route([router.url]);
var args=FilterRegisters.use(router.url,router.method).
concat(KLH(ContRegisters,"emit",router.guid));
ROU[router.method].apply(ROU,args);
});
App.use(apiCofig.baseUrl, Router);
/*View 设置*/
var Viewer=require("./Viewer");
var Viewer=new Viewer(App,require("handlebars"));
App.set('view engine', 'html');
App.engine('html', Viewer.engine);
/*加载中间间*/
apiCofig.use&&apiCofig.use(App);
var server = App.listen(apiCofig.port, function () {
var host = server.address().address;
var port = server.address().port;
});
}