-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverjs.js
45 lines (36 loc) · 891 Bytes
/
serverjs.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
var app, appendMessage, callbacks, express;
express = require("express");
callbacks = [];
appendMessage = function(message) {
var resp, results;
resp = {
messages: [message]
};
results = [];
while (callbacks.length > 0) {
results.push(callbacks.shift()(resp));
}
return results;
};
app = module.exports = express.createServer();
app.use(express.bodyParser());
app.get("/", function(req, res) {
return res.sendfile("index.html");
});
app.post("/send", function(req, res) {
var message;
message = {
nickname: req.param("nickname", "Anonymous"),
text: req.param("text", "")
};
appendMessage(message);
return res.json({
status: "ok"
});
});
app.get("/recv", function(req, res) {
return callbacks.push(function(message) {
return res.json(message);
});
});
app.listen(process.env.PORT);