-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequestHandlers.js
More file actions
69 lines (59 loc) · 2.04 KB
/
requestHandlers.js
File metadata and controls
69 lines (59 loc) · 2.04 KB
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
var querystring = require("querystring"),
fs = require("fs"),
url = require("url");
var db = require("./database");
db.start(); // Start connection to MongoDb
var math = {
"get" : function (res) {
console.log("Processing Request for math.get");
db.get("math", {}, function (err, docs) {
if (err) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end("Query error:" + err, "utf-8");
return;
}
res.writeHead(200, {"Content-Type": "text/html"});
res.end(JSON.stringify(docs), "utf-8");
});
},
"put" : function (res, req) {
console.log("Processing Request for math.put");
var urlparts = url.parse(req.url);
var querystring = urlparts.query.split("&");
var user = "";
var time = "";
var date = "";
var patt_user = /user=/;
var patt_time = /time=/;
var patt_date = /date=/;
querystring.forEach(function (item) {
if (patt_user.test(item)) {
user = item.replace(patt_user,"");
user = decodeURI (user);
user = user.replace(/\+/g," "); // TODO: better way to pass in " " in parameters like a post body.
user = user.substr(0,20); // For safety, truncate to 20 chars.
} else if (patt_time.test(item)) {
time = item.replace(patt_time,"");
time = decodeURI (time);
time = time.replace(/\+/g," "); // TODO: better way to pass in " " in parameters like a post body.
time = time.substr(0,20); // For safety, truncate to 20 chars.
} else if (patt_date.test(item)) {
date = item.replace(patt_date,"");
date = decodeURI (date);
date = date.replace(/\+/g," "); // TODO: better way to pass in " " in parameters like a post body.
date = date.substr(0,20); // For safety, truncate to 20 chars.
}
});
var data = {"user":user,"time":time,"date":date};
console.log(data);
db.put("math", data, function (err, docs) {
if (err) {
console.log("Insert error", err);
return;
}
});
res.writeHead(200, {"Content-Type": "text/html"});
res.end(JSON.stringify(data), "utf-8");
}
};
exports.math = math;