1 | var http = require("http");
|
2 | var express = require('express');
|
3 | var path = require('path');
|
4 |
|
5 |
|
6 |
|
7 | var alaEventHandler = require('./alaEventHandler')
|
8 | var middleware = require('./middleware');
|
9 | var configuration = require('./configuration');
|
10 | var router = require('./router')
|
11 | var ctrlHandler = require('./ctrlHandler');
|
12 | var viewHandler = require('./viewHandler');
|
13 |
|
14 |
|
15 | var db = require('./db');
|
16 |
|
17 | var 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);
|
26 |
|
27 | this.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();
|
37 |
|
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 |
|
94 | module.exports = Core;
|
95 |
|
96 |
|