/
server.js
106 lines (87 loc) · 2.66 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
96
97
98
99
100
101
102
103
104
105
106
var http = require("http"),
sys = require("sys"),
url = require("url"),
qs = require("querystring");
var ITEMS_BACKLOG = 20;
var urlMap = {
'/real_time_feed' : function (req, res) {
var since = parseInt(qs.parse(url.parse(req.url).query).since, 10);
feed.query(since, function (data) {
res.simpleJSON(200, data);
});
},
'/send_feed_item' : function (req, res, json) {
feed.appendMessage( json );
res.simpleJSON(200, {});
}
}
http.createServer(function (req, res) {
// Get the url and associate the function to the handler
// or
// Trigger the 404
handler = urlMap[url.parse(req.url).pathname] || notFound;
var json = "";
if(req.method === "POST"){
// We need to process the post but we need to wait until the request's body is available to get the field/value pairs.
req.body = '';
req.addListener('data', function (chunk) {
// Build the body from the chunks sent in the post.
req.body = req.body + chunk;
})
.addListener('end', function () {
json = JSON.stringify(qs.parse(req.body));
handler(req, res, json);
}
);
}else{
handler(req, res);
}
res.simpleJSON = function (code, obj) {
var body = JSON.stringify(obj);
res.writeHead(code, {
"Content-Type": "text/json",
"Content-Length": body.length
}
);
res.end(body);
};
}).listen(8001);
// This method handles the feed push and querying.
var feed = new function () {
var real_time_items = [],
callbacks = [];
this.appendMessage = function (json) {
// Append the new item.
real_time_items.push( json );
// Log it to the console
sys.puts(new Date() + ": " + JSON.parse(json).type + " pushed");
// As soon as something is pushed, call the query callback
while (callbacks.length > 0)
callbacks.shift().callback([JSON.parse(json)]);
// Make sur we don't flood the server
while (real_time_items.length > ITEMS_BACKLOG)
real_time_items.shift();
};
this.query = function (since, callback) {
var matching = [];
for (var i = 0; i < real_time_items.length; i++) {
var real_time_item = real_time_items[i];
if (real_time_item.timestamp > since)
matching.push(real_time_item)
}
if (matching.length != 0) {
callback(matching);
} else {
callbacks.push({ timestamp: new Date(), callback: callback });
}
};
};
var NOT_FOUND = "Not Found\n";
function notFound(req, res) {
res.sendwriteHeadHeader(404, [ ["Content-Type", "text/plain"]
, ["Content-Length", NOT_FOUND.length]
]);
res.write(NOT_FOUND);
res.end();
}
console.log('Server running at http://127.0.0.1:8001/');