This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
92 lines (78 loc) · 2.25 KB
/
routes.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
/*jslint node : true, continue : true,
devel : true, indent : 2, maxerr : 50,
newcap : true, nomen : true, plusplus : true,
regexp : true, sloppy : true, vars : false,
white : true
*/
/*global require, process, module, ObjectID */
var chat = require('./lib/chat.js');
var crud = require('./lib/crud.js');
module.exports = function(app, server) {
var routes = {
set: function() {
app.all('/:object/*?', function(req, res, next) {
var is_valid_object = (!! crud.schema_map[req.params.object]);
res.contentType('json');
if (is_valid_object) {
next();
}
else {
res.send(req.params.object + " is not a valid object type.");
}
});
app.get('/:object/list', function(req, res) {
crud.read(
req.params.object,
{},
{},
function(result) {
res.send(result);
});
});
app.post('/:object/create', function(req, res) {
crud.construct(
req.params.object,
req.body,
function(result) {
res.send(result);
});
});
app.get('/:object/read/:id', function(req, res) {
var find_map = {_id: ObjectID(req.params.id)};
crud.read(
req.params.object,
find_map,
{},
function(err, result) {
res.send(result);
});
});
app.post('/:object/update/:id', function(req, res) {
var find_map = {_id: ObjectID(req.params.id)};
var object_map = req.body;
var object_type = req.params.object;
crud.update(
object_type,
find_map,
object_map,
function(result) {
res.send(result);
});
});
app.get('/:object/delete/:id', function(req, res) {
var object_map = {_id: ObjectID(req.params.id)};
crud.destroy(
req.params.object,
object_map,
function(result) {
res.send(result);
});
});
app.get('/', function(req, res) {
res.redirect('/spa.html');
});
}
};
chat.connect(server);
return routes;
};