-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
87 lines (75 loc) · 2.25 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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var methodOverride = require("method-override");
var fs = require("fs");
var mongoose = require("mongoose");
var configure = require("./configure");
var repository = require("./repository");
var db = mongoose.connection;
db.once('open', function(callback) {
});
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
app.use(methodOverride());
mongoose.connect(configure.config.db.url, function(err, res) {
if (err) throw err;
console.log("Conected to polimedia");
configure.checkInitConfig();
repository.setup(router,app);
});
var router = express.Router();
router.get('/', function(req, res) {
res.send("Hello, World!");
});
var RESTFilesystem = {
routeFile: function(path) {
var endpointBase = path.replace(__dirname, "");
endpointBase = endpointBase.replace(".js","");
var routes = require(path).routes;
if (routes) {
for (var key in routes) {
var route = routes[key];
var method = null;
var callback = null;
var param = route.param;
for (method in route) {
var value = route[method];
if (typeof(value)=="function" || typeof(value)=="object") {
callback = value;
break;
}
}
var endpoint = endpointBase;
if (param) {
endpoint += '/:' + param;
}
console.log(endpoint + " > " + method);
router[method](endpoint,callback);
}
}
else {
console.log("Warning (" + endpointBase + "): No routes found");
}
},
routeDirectory: function(path) {
var dir = fs.readdirSync(path);
var This = this;
dir.forEach(function(entry) {
var itemPath = path + "/" + entry;
var stats = fs.lstatSync(itemPath);
if (stats.isDirectory()) {
This.routeDirectory(itemPath);
}
else {
This.routeFile(itemPath);
}
});
}
}
var restRoot = __dirname + '/rest';
RESTFilesystem.routeDirectory(restRoot);
app.use(router);
app.listen(configure.config.connection.port, function() {
console.log("Node server running on http://localhost:" + configure.config.connection.port);
});