-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
105 lines (81 loc) · 3.28 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
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
var port = process.env.PORT || 3000,
http = require('http'),
fs = require('fs'),
html = fs.readFileSync('index.html');
url = require('url');
// here we keep all the messages
var myLastMessage = [];
var server = http.createServer(function (req, res) {
// dealing with POST request
if (req.method === 'POST' && url.parse(req.url).pathname === "/chatroom/") {
var body = '';
// get the content of the body
req.on('data', function(chunk) {
body += chunk;
});
// when we are done with body
req.on('end', function() {
// get the id parameter from the url
var query = url.parse(req.url).query;
var id = query.replace('id=', '');
// create the message object
var result = {
id: id,
message: body,
}
// set the flag for checking if there is another message with the same id
var found = false;
// look for the last message for given id, if found -> replace,
// not found then just add
myLastMessage.find(function(element, index, array) {
if(element.id === id) {
found = true;
myLastMessage.splice(index, 1, result);
}
});
// at this point the id is not found
if(!found) myLastMessage.push(result);
res.writeHead(200, 'OK', {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
});
res.end("\n\nSuccess");
}); // end of req.on request
} else if (req.method === 'POST') {
res.writeHead(400, 'Bad request', {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
});
res.write(JSON.stringify({Error : "POST request not well formed."}));
res.end();
} // end of POST request
// dealing with GET request
if (req.method === 'GET' && url.parse(req.url).pathname === "/chatroom/") {
// get the id parameter from the url
var query = url.parse(req.url).query;
var id = query.replace('id=', '');
var lastMessage = {};
myLastMessage.find(function(element, index, array) {
if(element.id === id) {
lastMessage = element;
}
});
res.writeHead(200, 'OK', {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
});
res.write(JSON.stringify(lastMessage));
res.end();
} else if (req.method === 'GET') {
res.writeHead(200);
res.write(html);
res.end();
}
});
// Listen on port 3000, IP defaults to 127.0.0.1
server.listen(port);
// Put a friendly message on the terminal
console.log('Server running at http://127.0.0.1:' + port + '/');