-
Notifications
You must be signed in to change notification settings - Fork 20
/
resourceserver.js
85 lines (68 loc) · 2.74 KB
/
resourceserver.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
// Implements an in-memory resource server
module.exports = {
listen: function (port) {
var coffee = require('coffee-script'),
express = require('express'),
app = express(),
config = require('./config')
persist = config.use_redis ? require('./redispersist') : require('./persist'),
port = port || 3002,
_ = require('underscore'),
log = require('./logger')
;
app.configure(function () {
app.use(express.bodyParser());
app.use(allowCrossDomain);
});
app.get("/:collection", function(req, res) {
log.info('read ' + req.params.collection);
persist.all(req.params.collection).then(function (c) {
res.send(c);
}, function (e) {
res.send(500, e);
});
});
// create -> POST /collection
app.post('/:collection', function(req, res){
log.info('create ' + req.params.collection + '\n' + req.body);
persist.insert(req.params.collection, req.body).then(function (withId) {
res.send(withId);
}, function (e) {
res.send(500, e);
});
});
// read -> GET /collection[/id]
app.get('/:collection/:id?', function (req,res) {
log.info('read ' + (req.params.id || ('collection ' + req.params.collection)));
persist.get(req.params.collection, req.params.id).then(function (el) {
res.send(el);
}, function (e) {
res.send(500, e);
});
});
// update -> PUT /collection/id
app.put('/:collection/:id', function (req,res) {
log.info('update ' + req.params.collection + ':' + req.params.id);
updated = persist.update(req.params.collection, req.params.id, req.body);
res.send(updated);
});
// delete -> DELETE /collection/id
app.delete('/:collection/:id', function (req,res) {
log.info('delete ' + req.params.collection + ':' + req.params.id);
persist.delete(req.params.collection, req.params.id);
res.send(200);
});
app.listen(port);
log.info('Resource-server started at port %d.', port);
app.options("*", function (req, res) {
res.header('Allow', 'OPTIONS,GET,PUT,POST,DELETE');
res.send();
});
function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
}
}