forked from dalcib/angular-phonecat-mongodb-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (82 loc) · 3.25 KB
/
server.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
var express = require('express'),
app = express.createServer(),
db = require('mongojs').connect('PhoneCat');
app.configure(function () {
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.logger('dev')); //tiny, short, default
app.use(app.router);
app.use(express.static(__dirname + '/app'));
app.use(express.errorHandler({dumpExceptions: true, showStack: true, showMessage: true}));
});
/* Helpers */
//To allow use ObjectId or other any type of _id
var objectId = function (_id) {
if (_id.length === 24 && parseInt(db.ObjectId(_id).getTimestamp().toISOString().slice(0,4), 10) >= 2010) {
return db.ObjectId(_id);
}
return _id;
}
//Function callback
var fn = function (req, res) {
res.contentType('application/json');
var fn = function (err, doc) {
if (err) {
if (err.message) {
doc = {error : err.message}
} else {
doc = {error : JSON.stringify(err)}
}
}
if (typeof doc === "number" || req.params.cmd === "distinct") { doc = {ok : doc}; }
res.send(doc);
};
return fn;
};
/* Routes */
// Query
app.get('/api/:collection', function(req, res) {
var item, sort = {}, qw = {};
for (item in req.query) {
req.query[item] = (typeof +req.query[item] === 'number' && isFinite(req.query[item]))
? parseFloat(req.query[item],10)
: req.query[item];
if (item != 'limit' && item != 'skip' && item != 'sort' && item != 'order' && req.query[item] != "undefined" && req.query[item]) {
qw[item] = req.query[item];
}
}
if (req.query.sort) { sort[req.query.sort] = (req.query.order === 'desc' || req.query.order === -1) ? -1 : 1; }
db.collection(req.params.collection).find(qw).sort(sort).skip(req.query.skip).limit(req.query.limit).toArray(fn(req, res))
});
// Read
app.get('/api/:collection/:id', function(req, res) {
db.collection(req.params.collection).findOne({_id:objectId(req.params.id)}, fn(req, res))
});
// Save
app.post('/api/:collection', function(req, res) {
if (req.body._id) { req.body._id = objectId(req.body._id);}
db.collection(req.params.collection).save(req.body, {safe:true}, fn(req, res));
});
// Delete
app.del('/api/:collection/:id', function(req, res) {
db.collection(req.params.collection).remove({_id:objectId(req.params.id)}, {safe:true}, fn(req, res));
});
//Group
app.put('/api/:collection/group', function(req, res) {
db.collection(req.params.collection).group(req.body.keys, req.body.cond, req.body.initial, req.body.reduce, req.body.finalize, fn(req, res))
})
// MapReduce
app.put('/api/:collection/mapReduce', function(req, res) {
if (!req.body.options) {req.body.options = {}};
req.body.options.out = { inline : 1};
req.body.options.verbose = false;
db.collection(req.params.collection).mapReduce(req.body.map, req.body.reduce, req.body.options, fn(req, res));
})
// Command (count, distinct, find, aggregate)
app.put('/api/:collection/:cmd', function (req, res) {
if (req.params.cmd === 'distinct') {req.body = req.body.key}
db.collection(req.params.collection)[req.params.cmd](req.body, fn(req, res));
})
app.listen(8000, function() {
console.log("Listening on 8000");
});