-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloc_loader.js
58 lines (44 loc) · 1.62 KB
/
loc_loader.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
var exports;
var mysql = require("mysql");
var fs = require('fs');
var config = require('./config.js');
var utils = require('./util.js')
var connection = mysql.createConnection({
host: config.dbserver,
user: config.dbuser,
password: config.dbpass,
database: config.dbName
});
function loadLocaterScripts(requestContext){
"use strict";
var acctId = requestContext.url.substring(9,requestContext.url.length - 3);
fs.readFile('./scripts/locator.js','utf8',function(error,rsp){
//check error
rsp = rsp.replace("{ACCT_ID}",acctId);
rsp = rsp.replace("{G_API_KEY}",config.gApiKey);
//rsp = rsp.replace("{G_MAP_SENSOR}","false");
rsp = rsp.replace("{LOCATOR_STYLES}",fs.readFileSync('./css/locator.css','utf8'));
connection.query('SELECT * FROM locations', function (error, rows) {
//check error
//console.log(JSON.stringify(rows));
connection.end();
rsp = rsp.replace("{LOCATION_DATA}",JSON.stringify(rows));
requestContext.response.writeHead(200, {'Content-Type': 'application/javascript'});
requestContext.response.end(rsp);
});
});
return true;
}
exports.handleRequest = function (requestContext) {
var handled = false;
console.log(requestContext.url.substring(1,8));
if (requestContext.url.substring(1,8) != "locator"){
return false;
} else {
handled = loadLocaterScripts(requestContext);
}
if (!handled){
utils.returnUnknownRequest(request, response, url);
}
return true;
}