UNPKG

2.15 kBJavaScriptView Raw
1var http = require("http");
2var express = require('express');
3var path = require('path');
4
5
6/*-------------- my require ---------------*/
7var alaEventHandler = require('./alaEventHandler')
8var middleware = require('./middleware');
9var configuration = require('./configuration');
10var router = require('./router')
11var ctrlHandler = require('./ctrlHandler');
12var viewHandler = require('./viewHandler');
13
14// var tools = require('./alajsTools');
15var db = require('./db');
16/*------------------- 核心 -----------------*/
17var Core = function(appName){
18 var app;
19 this.appName = appName;
20 this.rootPath = path.dirname(require.main.filename);
21 this.controllers = {}; /*控制器对象集合*/
22 this.models = {};
23
24 this.bootstrap = function(){
25 alaEventHandler(this); /*框架事件handler*/
26
27 this.express = express(); /*启动express*/
28 app = this.express;
29
30 configuration(this);/*配置文件*/
31 middleware(this);/*中间件管理*/
32 router = router(this);
33 ctrlHandler(this);
34 viewHandler(this);
35 db(this);
36 this.createHttp();/*启动http服务*/
37 // tools(this);
38 }
39
40 this.get = function(configName){
41 return app.get(configName);
42 }
43 this.set = function(configName,value){
44 app.set(configName,value)
45 }
46
47
48 this.createHttp = function(){
49 app.set('port',process.env.PORT || app.get('port'));
50 app.listen(this.get('port'));
51 console.log("[Server Start]listen on --->".success+" http://127.0.0.1:".red+this.get('port').toString().red);
52 }
53
54 this.route = function(method,path,callbacks){
55 if(method == 'get'){
56 app.get(path,function(req,res,next){
57 callbacks(req,res,this)
58 });
59 }
60 else if(method == 'post'){
61 app.post(path,callbacks);
62 }
63 else if(method == 'put'){
64 app.put(path,callbacks);
65 }
66 else if(method == 'delete'){
67 app.delete(path,callbacks);
68 }else{
69 alajs.echoError(method +' method 不存在!!!')
70 }
71 }
72
73 this.use = function(middle){
74 app.use(middle);
75 }
76
77
78 this.getModel = function(modelName){
79 if(typeof this.models[modelName] == 'function'){
80 return this.models[modelName];
81 }else{
82 alajs.echoError(modelName + " 模型不存在");
83 return null
84 }
85 }
86
87
88
89
90}
91
92
93
94module.exports = Core;
95
96