-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
184 lines (159 loc) · 6.84 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var http = require('http');
var querystring = require('querystring');
var util = require('util');
var fs = require("fs");
var file = "food_tracker.db";
var exists = fs.existsSync(file);
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database(file);
// Create database
db.serialize(function() {
if(!exists) {
console.log("Create database");
db.run("CREATE TABLE Tracker(id INT, menu TEXT, data TEXT)");
db.run("CREATE TABLE Users(id INT, name TEXT)");
// save last ID into database
db.run("INSERT INTO Tracker VALUES (1, 'Snitel', '20160204_203424')");
db.run("INSERT INTO Tracker VALUES (1, 'Shaorma', '20160127_162512')");
db.run("INSERT INTO Tracker VALUES (1, 'Burger', '20160205_145043')");
db.run("INSERT INTO Tracker VALUES (1, 'Pizza', '20160206_175032')");
db.run("INSERT INTO Tracker VALUES (1, 'Burger', '20160205_145043')");
db.run("INSERT INTO Users VALUES (1, 'LAST_ID')");
db.run("INSERT INTO Users VALUES (1, 'Irina')");
} else {
console.log("Database already created");
}
});
//Lets define a port we want to listen to
const PORT=8080;
//We need a function which handles requests and send response
function handleRequest(request, response){
console.log("handleRequest");
switch(request.url) {
case '/':
console.log("From website");
if (request.method == 'GET') {
response.writeHead(200, "OK", {'Content-Type': 'text/html'});
response.write('<html><head><title>Welcome</title></head><body>');
response.write('<h1>Welcome to Food Tracker History!</h1>');
response.write('<h2>Enter your name to view your food history</h2>');
response.write('<form enctype="application/x-www-form-urlencoded" action="/show" method="post">');
response.write('Name: <input type="text" name="username" /><br />');
response.write('<input type="submit" value="Find my meals!"/>');
response.write('</form></body></html');
response.end();
}
case '/show':
console.log("Show history");
if (request.method == 'POST') {
var fullBody = '';
var json;
request.on('data', function(chunk) {
fullBody += chunk.toString();
});
request.on('end', function() {
var data = querystring.parse(fullBody);
console.log("History for " + data.username);
db.all("SELECT * FROM Users WHERE name == (?)", [data.username], function (err, dbRes) {
if (err) { throw err; }
response.writeHead(200, "OK", {'Content-Type': 'text/html'});
response.write('<html><head><title>History</title></head><body>');
response.write('<h1>Here is the history for ' + data.username + '</h1>');
db.all("SELECT menu, data FROM Tracker WHERE id == (?)", [dbRes[0].id], function (errr, result) {
json = JSON.stringify(result, null, 4);
console.log("FROM DB: %s", json);
//response.writeHead(200, { 'Content-Type': 'test/json'});
//response.write(json);
for (var i = 0; i < result.length; i++) {
response.write('<h2>' + result[i].menu + ' at time: ' + result[i].data + '</h2>');
}
response.end();
});
});
});
}
break;
case '/register':
console.log("Register new user");
if (request.method == 'POST') {
console.log("Received request POST");
var body = '';
request.on('data', function(chunk) {
body += chunk;
});
request.on('end', function() {
var data = querystring.parse(body);
console.log("Name: %s", data.paramUsername);
// send id
var x;
db.each("SELECT id FROM Users WHERE name == 'LAST_ID'", function(err, id) {
var upd = db.prepare("UPDATE Users SET id = (?) WHERE name == 'LAST_ID'");
x = id.id + 1;
console.log("New id is ", x);
upd.run(x);
upd.finalize();
// add user to database
var add = db.prepare("INSERT INTO Users VALUES (?, ?)");
add.run(x, data.paramUsername);
add.finalize();
var y = x + "";
console.log("y=%s, x = %s",y, x);
response.end(y);
});
});
}
break;
case '/new_menu':
console.log("Add a new menu");
if (request.method == 'POST') {
console.log("Received request POST");
var body = '';
request.on('data', function(chunk) {
body += chunk;
});
request.on('end', function() {
var data = querystring.parse(body);
var id = data.paramId;
var food = data.paramFood;
var time = data.paramTime;
console.log("Inset into database for id %s: food = %s, time = %s", id, food, time);
var upd = db.prepare("INSERT INTO Tracker VALUES (?, ?, ?)");
upd.run(parseInt(id), food, time);
upd.finalize();
});
response.end('0');
}
break;
case '/history':
console.log("Retrieve history");
if (request.method == 'POST') {
console.log("Received request POST");
var body = '';
request.on('data', function(chunk) {
body += chunk;
});
request.on('end', function() {
var data = querystring.parse(body);
var id = data.paramId;
db.all("SELECT * FROM Tracker WHERE id == (?)", [parseInt(id)], function (err, result) {
if (err) { throw err; }
json = JSON.stringify(result);
console.log("FROM DB: %s", json);
response.end(json);
});
});
}
break;
default:
response.writeHead(404, "Not found", {'Content-Type': 'text/html'});
response.end('<html><head><title>404 - Not found</title></head><body><h1>Not found.</h1></body></html>');
break;
}
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log("Server listening on: http://localhost:%s", PORT);
});