-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-test.js
81 lines (68 loc) · 1.71 KB
/
server-test.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
/*
* Copyright Intel 2013 Michael Wood <michael.g.wood@intel.com>
*/
/* test curl http://127.0.0.1:8080/getgeolocation?lat=51.507222&lng=-0.1275 */
"use strict";
var http = require ("http");
var url = require ("url");
var sqlite = require ('sqlite3').verbose ();
var querystring = require ('querystring');
var geolocator = require ('./geolocator.js');
var port = 8080;
function printError (error)
{
if (error)
console.log (error);
}
function endResponse (response, error)
{
if (error) {
console.log (error);
response.write ('{ "return" : "1" , "error" : "'+error+'" }');
} else {
response.write ('{ "return" : "0" }');
}
response.end ();
}
/* Callback function for successful getGeolocation
*/
function addGeolocationToFile (queryData, loc)
{
console.log ("Location done");
}
function getGeolocation (queryData, response)
{
var geolocatorData;
geolocatorData = geolocator.newGeolocationData (queryData, addGeolocationToFile);
geolocator.getGeolocation (geolocatorData, response);
}
function route (pathname, queryData, response)
{
if (queryData)
{
switch (pathname)
{
case '/getgeolocation' :
getGeolocation (queryData, response);
break;
default:
endResponse (response, "404");
break;
}
}
}
function main ()
{
geolocator.load ();
http.createServer (function (request, response) {
response.writeHead (200, { "Content-Type": "text/plain" });
var parsedUrl = url.parse (request.url, true);
console.log (parsedUrl.pathname + parsedUrl.query);
route (parsedUrl.pathname, parsedUrl.query, response);
}).listen (port);
process.on ('SIGINT', function () {
console.log ("..OK BYE");
process.exit (0);
});
}
main ();