-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (73 loc) · 2.22 KB
/
index.js
File metadata and controls
87 lines (73 loc) · 2.22 KB
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
(function (factory) {
var _;
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined' && typeof require !== 'undefined') {
// CommonJS
_ = require('underscore')
_.extend(module.exports, factory(_, require('express'), require('url')));
} else {
// running in browser
_ = this._;
_.extend(window, factory(_, {}, {}));
}
})(function(_, express, url) {
function respond(promise, res) {
promise
.then(function (result) {
res.send(JSON.stringify(result));
})
.fail(function (err) {
res.statusCode = err.code || 500;
res.send(String(err));
});
}
function _applyRoutes(app, name, store) {
app.use(express.bodyParser());
name = name.replace(/^\/?/, '/');
app.get(name, function(req, res) {
respond(store.query(url.parse(req.url).query), res);
});
app.get(name+'/:id', function(req, res) {
respond(store.get(req.params.id), res);
});
app.post(name, function(req, res) {
respond(store.add(req.body), res);
});
app.put(name+'/:id', function(req, res) {
respond(store.put(req.body, req.params.id), res);
});
app.del(name, function(req, res) {
respond(store.clear(), res);
});
app.del(name+'/:id', function(req, res) {
respond(store.delete(req.params.id), res);
});
}
function applyRoutes(app, name, store) {
if (arguments.length === 2) {
if (name instanceof Array) {
// app, array of stores
for (var i = 0; i < name.length; i++) {
_applyRoutes(app, name[i].name, name[i]);
};
} else if (name.name && typeof name.get === 'function') {
// app, store
_applyRoutes(app, name.name, name);
} else {
// app, object with stores
for (a in name) {
if (name.hasOwnProperty) {
_applyRoutes(app, a, name[a]);
}
}
}
} else if (arguments.length === 3) {
// app, name, store
_applyRoutes(app, name, store);
}
return app; // allow chaining
}
return {
applyRoutes: applyRoutes,
respond: respond
};
});