-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
45 lines (36 loc) · 1.06 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
var http = require("http");
var mysql = require("db-mysql");
var querystring = require("querystring");
var url = require("url");
function onRequest(request, response) {
console.log("Request received.");
var query = url.parse(request.url).query;
var queryTerm = querystring.parse(query)['term'];
console.log(query);
console.log(queryTerm);
response.writeHead(200, {
'Content-Type' : 'x-application/json'
});
var json = '';
new mysql.Database({
hostname: 'localhost',
user: 'root',
password: 'r',
database: 'Gsoc'
}).connect(function(error) {
if (error) {
console.log("CONNECTION ERROR: " + error);
}
this.query('SELECT * FROM doctorModule WHERE Text LIKE ?',['%'+queryTerm+'%']).execute(function(error, rows) {
if (error) {
console.log('ERROR: ' + error);
}
console.log(rows.length + ' ROWS');
json = JSON.stringify(rows.items);
console.log('JSON-result:', json);
response.end(json);
});
});
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");