-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (41 loc) · 1.17 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
var express = require('express');
var bodyParser = require('body-parser');
var MongoClient = require('mongodb').MongoClient;
var config = require('./config');
var route = require('./route')(config.routes);
var middleware = require('./middleware')(route, config);
MongoClient.connect(config.mongo.url, function(err, db) {
if (err) {
console.log('Can not connect to MongoDB');
throw err;
}
var service = require('./service')(route, db);
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended : true
}));
app.get('*', middleware.token, middleware.isAvailableRoute, function(req, res) {
route.getRoute(req.path, function(err, route) {
if (err) {
return res.send(500);
}
if (!route) {
return res.send(404);
}
service.call(route, req, function(err, data) {
if (err) {
return res.send(500, err);
}
return res.json(data);
});
});
});
app.listen(config.port || 3000, function(err) {
if (err) {
throw err;
}
console.log('WOA Cache Frontend is running on', config.port);
console.log('Configured with', config);
});
});