-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (68 loc) · 2.36 KB
/
app.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
// Example of a Backbone client app with NodeJS server
// Copyright by Javier Arevalo in 2012.
//
// - http://www.iguanademos.com/Jare/
// - @TheJare on twitter
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
var express = require('express')
, _ = require('underscore')
, path = require('path')
// Simple express server setup
var app = express.createServer()
app.use(express.static(path.join(__dirname, 'static')))
app.use(express.logger())
app.use(express.bodyParser())
// Our 'database'
// A dictionary where the key is an 'id' and the value is an object that contains that id as a field named 'id'.
var users = {}
// Our REST-oriented api
// Collection-level
// Fetch the entire collection
app.get('/api/usersCollection', function(req, res) {
console.log("usersCollection api: ", req.method, req.url, req.params, req.body);
res.send(_.values(users));
console.log(users);
});
// Update the entire collection
app.put('/api/usersCollection', function(req, res) {
console.log("usersCollection api: ", req.method, req.url, req.params, req.body);
_.each(req.body, function(e) { if ('id' in e) users[e.id] = e; });
res.send(200);
console.log(users);
});
// Item-level
// Add item to the collection
app.post('/api/usersCollection', function(req, res) {
console.log("usersCollection api: ", req.method, req.url, req.params, req.body);
// Create and assign an id for the new item
var uid;
do {
uid = _.uniqueId("id_");
} while (uid in users);
users[uid] = req.body;
users[uid].id = uid;
res.send({ id: uid });
//console.log(users);
});
// Update item
app.put('/api/usersCollection/:id', function(req, res) {
console.log("usersCollection api: ", req.method, req.url, req.params, req.body);
users[req.param('id')] = req.body;
res.send(200);
//console.log(users);
});
// Remove item
app.del('/api/usersCollection/:id', function(req, res) {
console.log("usersCollection api: ", req.method, req.url, req.params, req.body);
delete users[req.param('id')];
res.send(200);
//console.log(users);
});
// -- Log other types of requests for debugging
app.all('/api/*', function(req, res) {
console.log("api: ", req.method, req.url, req.params, req.body);
res.send("Unsupported API", 404);
});
app.listen(3000)
console.log("Express listening on http://localhost:3000/");