This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (55 loc) · 1.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
var express = require('express'),
app = express.createServer(),
mysql = require('mysql'),
url = require('url');
var conn = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
socketPath : '/var/mysql/mysql.sock',
database: 'RHoK_NDWI'
});
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
conn.connect();
app.get('/', function(req, res) {
res.render('index.ejs')
});
app.get('/api', function(req, res) {
res.render('api');
});
app.get('/api/latLong/:lat/:long', function(req, res){
var latitude = req.params.lat,//'51.9947'
longitude = req.params.long;//'-1.4976'
conn.query('SELECT * FROM Data WHERE `longitude` = "' + longitude + '" AND `latitude` = "' + latitude + '"', function(err, rows, fields) {
if (err) throw err;
res.send(rows);
});
});
app.get('/api/squareLatLong/:latOne/:longOne/:latTwo/:longTwo', function(req, res){
var pointALatitude = req.params.latOne,//'51.9947'
pointALongitude = req.params.longOne,//'-1.4976'
pointBLatitude = req.params.latTwo,//'51.9947'
pointBLongitude = req.params.longTwo;//'-1.4976'
var url_parts = url.parse(req.url, true);
var getQuery = url_parts.query;
var SQLQuery = 'SELECT ';
if (getQuery.select) {
SQLQuery += getQuery.select + ' ';
} else {
SQLQuery += '* ';
};
SQLQuery += 'FROM Data ';
SQLQuery += 'WHERE `longitude` <= "' + pointALatitude + '" AND `latitude` >= "' + pointBLatitude + '" AND `longitude` <= "' + pointALongitude + '" AND `longitude` >= "' + pointBLongitude + '"'
if (getQuery.from) {
SQLQuery += 'AND `timeTaken` >= "' + getQuery.from + '" ';
if (getQuery.till) {
SQLQuery += 'AND `timeTaken` <= "' + getQuery.till + '" ';
};
};
conn.query(SQLQuery, function(err, rows, fields) {
if (err) throw err;
res.send(rows);
});
});
app.listen(3000);