-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandlers.js
44 lines (37 loc) · 1.19 KB
/
requestHandlers.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
var mysql = require("mysql");
var config = require("./config");
var connection = mysql.createConnection(config.getConfig().db);
connection.connect();
function root(response, postData){
// console.log("Request handler 'root' was called.");
try {
var data = JSON.parse(postData);
if (data.event_id === undefined){
// console.log(postData);
response.writeHead(500, {"Content-Type":"text/plain"});
response.write("500 Invalid JSON received");
response.end();
return;
}
} catch(er){
console.log('not json');
response.writeHead(500, {"Content-Type":"text/plain"});
response.write("500 Invalid JSON received");
response.end();
return;
}
var sql = "INSERT INTO message (message, event_id) VALUES (?, ?)";
connection.query(sql, [postData, data.event_id], function(error, results, fields){
});
response.writeHead(200, {"Content-Type": "text/plain"});
response.write('OK');
response.end();
}
function defaultHandler(response, postData){
console.log("Request handler 'defaultHandler' was called.");
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not found");
response.end();
}
exports.root = root;
exports.defaultHandler = defaultHandler;