-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.js
146 lines (111 loc) · 4.69 KB
/
welcome.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
//http://stackoverflow.com/questions/6011984/basic-ajax-send-receive-with-node-js
//http://codeforgeek.com/2014/07/node-sqlite-tutorial/
//https://github.com/mapbox/node-sqlite3/wiki/API
//DISCLAIMER: this is my first node app. I would not treat this as THE WAY to write node. For that, you might try howtonode.org.
// I threw this together in a hurry with internet searches, duck tape, and bailing wire.
// The point of this is more to illustrate what can be done than how to do it.
var http = require('http'),
fs = require('fs'),
url = require('url'),
sqlite3 = require('sqlite3').verbose()/*,
qs = require('querystring')*/;
(function () {
var db = new sqlite3.Database('guest.sqlite');
db.serialize(function() {
var error = '';
db.run("CREATE TABLE if not exists guests (id INTEGER PRIMARY KEY, email TEXT, firstName TEXT, lastName TEXT);");
});
//Passing a parameter to this function assigns a close handler
db.close();
})();
http.createServer(
/**
@param {object} request a http.IncomingMessage
@param {object} response a http.ServerResponse
*/
function(request, response){
var path = url.parse(request.url).pathname;
var guestPath = /\/guest\/([a-zA-z\-0-9]*)/;
if (request.method == 'POST' && path == '/guest') {
console.log('receieved request to create guest');
var body = '';
var guest = null;
request.on('data', function (data) {
console.log('Buffering data');
body += data;
// Too much POST data, kill the connection!
if (body.length > 1e6) {
request.connection.destroy();
}
});
request.on('end', function () {
console.log('Parsing data');
guest = JSON.parse(body);
if (guest) {
var db = new sqlite3.Database('guest.sqlite');
db.serialize(function() {
var stmt = db.prepare("INSERT INTO guests (email, firstName, lastName) VALUES (?,?,?);");
stmt.run(guest.email, guest.firstName, guest.lastName);
stmt.finalize();
//TODO: How do we check for errors here? try/catch?
console.log('Inserted row');
//This is blocking. To make it non-blocking, stick it outside request.on
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end();
});
//Passing a parameter to this function assigns a close handler
db.close();
}
});
}
else if (path == '/guests') {
console.log('Received request for guests')
var guests = [];
var db = new sqlite3.Database('guest.sqlite');
db.serialize(function() {
var error = '';
// Maybe should have used db.all here?
db.each("SELECT id, firstName, lastName, email FROM guests;",
function (err, row) {
//Row callback
if (err) {
console.log(err);
error = "Error occurred";
}
else {
console.log(row.email);
guests.push({id: row.id, email: row.email, firstName: row.firstName, lastName: row.lastName, fullName: row.firstName + ' ' + row.lastName});
}
},
function () {
//Completion callback
if (error) {
response.writeHead(500, { 'Content-Type': 'text/plain' });
response.end();
}
else {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(guests), 'utf-8');
}
});
});
//Passing a parameter to this function assigns a close handler
db.close();
}
else if (request.method == 'DELETE' && guestPath.test(path)) {
var id = guestPath.exec(path)[1];
var db = new sqlite3.Database('guest.sqlite');
db.serialize(function() {
var stmt = db.prepare("DELETE FROM guests WHERE email = ?;");
stmt.run(id);
//TODO: How do we check for errors here? try/catch?
response.writeHead(200, { 'Content-Type': 'text/plain' });
response.end();
});
}
else {
response.writeHead(500, { 'Content-Type': 'text/plain' });
response.end("Unrecognized Path", "utf-8");
}
}).listen(8001);
console.log("server initialized");