App.js
3.65 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var path = require('path');
var express = require('express');
var App = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var multer = require('connect-multiparty');
var Glob = require("glob");
var Utils=require('./Utils');
var Router = express.Router();
module.exports = function (apiCofig) {
// 记录下当前文档的路径
//global.apps=apiCofig.apps;
var Register={};
if(apiCofig.EnvConst){
for(var name in apiCofig.EnvConst){
Register[name]=apiCofig.EnvConst[name];
if(typeof Register[name]=="object"){
for(var key in Register[name]){
if(typeof Register[name][key]==="string"){
Register[name][key]=Utils.template(Register[name][key],Register)
}
}
}
}
}
App.set("Register",Register);
/*日志配置和Console*/
var Console = require("./Console");
Console(apiCofig.log);
console.info("YOHO");
global.Register=App.get("Register");
/*接口层*/
var Interfacer = require("./Interfacer");
var InterRegisters = new Interfacer(apiCofig,App);
apiCofig.MVC.Interfacer &&
Glob.sync(apiCofig.MVC.Interfacer).forEach(function (src) {
InterRegisters.register(require(src));
});
/*控制器层*/
var Controller = require("./Controller");
var ContRegisters = new Controller(InterRegisters);
apiCofig.MVC.Controller &&
Glob.sync(apiCofig.MVC.Controller).forEach(function (src) {
require(src)(ContRegisters);
});
/*过滤器*/
var Filter = require("./Filter");
var FilterRegisters = new Filter(InterRegisters);
apiCofig.MVC.filters &&
Glob.sync(apiCofig.MVC.filters).forEach(function (src) {
require(src)(FilterRegisters);
});
/*View 设置*/
var Viewer=require("./Viewer");
var ViewerRegisters=new Viewer(App, apiCofig.MVC.views);
var template = require('art-template');
template.config('base', '');
template.config('extname', '.html');
template.config('openTag', '<%');
template.config('closeTag', '%>');
App.engine('.html', template.__express);
App.set('view engine', 'html');
/*内置中间件 对Http请求解析*/
App.use(bodyParser.json());
App.use(bodyParser.urlencoded({extended: true}));
App.use(cookieParser());
App.use(multer());
App.use("/" + apiCofig.static.name, express.static(apiCofig.static.value));
/*前置中間件*/
apiCofig.useToEntry && apiCofig.useToEntry(App);
App.use(function(req, res, next){
ViewerRegisters.overrideRes.call(res);
next();
});
/*路由控制*/
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);
App.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
// console.error(err.message);
next(err);
});
/*後置中间间*/
apiCofig.useToOuter && apiCofig.useToOuter(App, ContRegisters);
App.use(function (req, res, next) {
res.end();
});
var server = App.listen(apiCofig.port, function () {
var host = server.address().address;
var port = server.address().port;
console.log("HOST:"+host+"PORT:"+port);
});
};