-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (82 loc) · 3.01 KB
/
index.js
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
/**
* Module dependencies.
*/
var express = require('express');
var http = require('http');
var path = require('path');
var conf = require('modulojs-conf');
var moduloJS = function(server) {
return this;
}
// Dynamic loading of app's middleware
moduloJS.loadModules = function(server,path,mode) {
var fs = require("fs");
fs.readdirSync(path).forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (!stat.isFile()) {
if (/modulojs(.*)/.test(file)) {
switch(mode) {
case "conf":
var mod = require(newPath);
if ( mod.conf ){
console.log("Init ["+mode+"] from module ["+file+"]");
mod.conf(server);
}else{
console.log("Ignoring ["+mode+"] from module ["+file+"] no method.");
}
break;
case "routing":
var mod = require(newPath);
if ( mod.initMod ){
console.log("Init ["+mode+"] from module ["+file+"]");
mod.initMod(server);
}else{
console.log("Ignoring ["+mode+"] from module ["+file+"] no method.");
}
break;
default:
throw new Exception('Unkown method','['+mode+']','for module ','['+path+']');
break;
}
}
}
});
};
moduloJS.conf = conf;
moduloJS.configure = conf.configure;
moduloJS.beforeRouting = conf.beforeRouting;
moduloJS.routing = conf.routing;
moduloJS.afterRouting = conf.afterRouting;
// Initialisz needed variables and routes for the app..
// As module or standalone..
moduloJS.initApp = function(server,mypath) {
var app = express();
app.conf = server.conf;
app.use(express.cookieParser());
app.use(express.session({
secret: 'secret'
}));
app.use(express.urlencoded());
app.use(express.methodOverride());
// Loading alls routes and middlewares
moduloJS.configure(app,mypath);
moduloJS.beforeRouting(app,mypath);
moduloJS.routing(app,mypath);
moduloJS.afterRouting(app,mypath);
app.use(app.router);
server.use(app);
}
if (require.main === module) {
console.log("I'm a main app... start listening");
var app = express();
var mypath = mypath || __dirname;
app.conf = moduloJS.conf;
app.set('port', process.env.PORT || 3000);
moduloJS.initApp(app,mypath);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
}
//adding line so this app can be himself a module for another app
module.exports = moduloJS;