-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlocal-server.js
143 lines (114 loc) · 2.78 KB
/
local-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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var http = require('http');
var uid = '4fc31f64e4b0769539c32f7e'.split('');
var nextUid = function() {
var index = uid.length;
var digit;
while(index) {
index--;
digit = uid[index].charCodeAt(0);
if (digit == 57 /*'9'*/) {
uid[index] = 'A';
return uid.join('');
}
if (digit == 90 /*'Z'*/) {
uid[index] = '0';
} else {
uid[index] = String.fromCharCode(digit + 1);
return uid.join('');
}
}
uid.unshift('0');
return uid.join('');
}
var DbStore = function() {
var store = [];
var map = {};
this.create = function(data) {
data._id = {
$oid: nextUid()
};
store.push(data);
map[data._id.$oid] = data;
return data;
};
this.getAll = function() {
return store;
};
this.remove = function(id) {
var item = map[id];
if (!item) return false;
delete map[id];
store.splice(store.indexOf(item), 1);
return true;
};
this.update = function(id, data) {
var item = map[id];
if (!item) return false;
for (var key in data) {
if (data.hasOwnProperty(key)) {
item[key] = data[key];
}
}
return item;
};
};
var db = new DbStore();
db.create({text: 'Try AngularJs', done: false});
db.create({text: 'Visit Boston', done: true});
db.create({text: 'Drink some beers', done: false});
var handler = {
GET: function(req, resp) {
resp.writeHead(200);
resp.write(JSON.stringify(db.getAll()));
resp.end();
},
PUT: function(req, resp, id) {
var data = '';
req.on('data', function(buffer) {
data += buffer.toString();
});
req.on('end', function() {
var item = db.update(id, JSON.parse(data));
if (item) {
resp.writeHead(200);
resp.write(JSON.stringify(item));
} else {
resp.writeHead(404);
}
resp.end();
});
},
POST: function(req, resp) {
var data = '';
req.on('data', function(buffer) {
data += buffer.toString();
});
req.on('end', function() {
resp.writeHead(201);
var item = db.create(JSON.parse(data));
resp.write(JSON.stringify(item));
resp.end();
});
},
DELETE: function(req, resp, id) {
resp.writeHead(db.remove(id) ? 200 : 404);
resp.end();
},
OPTIONS: function(req, resp) {
resp.writeHead(200, {
'Access-Control-Allow-Headers': 'origin, x-requested-with, accept, content-type',
'Access-Control-Allow-Methods': ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE'],
'Access-Control-Max-Age': '1728000',
'Allow': ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE']
});
resp.write(JSON.stringify(db.getAll()));
resp.end();
}
};
http.createServer(function(request, response) {
// CORS
response.setHeader('Access-Control-Allow-Credentials', 'true');
response.setHeader('Access-Control-Allow-Origin', '*');
var id = /items\/([a-z0-9]*)/.exec(request.url);
handler[request.method](request, response, id && id[1]);
}).listen(80);