-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (107 loc) · 3.31 KB
/
app.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
/* Weatherman */
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// Get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// Provides http requests
var request = require('request');
// Provides access to SQL database
var ibmdb = require('ibm_db');
// Parses request body
var bodyParser = require('body-parser');
// Create a new express server
var app = express();
app.use(bodyParser.json());
// Serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// Database connection settings
var db = {
db: "SQLDB",
hostname: "75.126.155.153",
port: 50000,
username: "user17167",
password: "bvRyojZUy1qT"
};
var connString = "DRIVER={DB2};DATABASE=" + db.db +
";UID=" + db.username +
";PWD=" + db.password +
";HOSTNAME=" + db.hostname +
";port=" + db.port +
";PROTOCOL=TCPIP";
app.get('/weather', function(req, res) {
var options = {
language: "en-US",
units: "e"
};
var url = 'https://9bec92cf-9cef-402d-ad65-5c87126bbfba:[email protected]';
var endPoint = '/api/weather/v2/observations/current';
var geocode = "?geocode=" + encodeURIComponent(req.query.latitude + "," + req.query.longitude);
var language = "&language=" + encodeURIComponent(options.language);
var units = "&units=" + encodeURIComponent(options.units);
var callUrl = url + endPoint + geocode + language + units;
request.get(callUrl, { json: true }, function(err, response, body) {
if (err) {
res.json({ success: false });
}
else {
res.json({ icon_code: body.observation.icon_code });
}
});
});
app.get('/highscores', function(req, res) {
ibmdb.open(connString, function(err, conn) {
if (err) {
console.log(err);
}
else {
var queryString = "SELECT Username, Score FROM Highscores";
conn.query(queryString, function(err, rows, moreResultSets) {
if (err)
console.log(err);
conn.close(function() {
console.log(rows);
res.send("Worked");
});
});
}
});
});
app.post('/highscores', function(req, res) {
ibmdb.open(connString, function(err, conn) {
if (err) {
res.json({ success: false });
}
else {
var queryStringInsert = "INSERT INTO HIGHSCORES (Username, Score) VALUES ('" +
req.body.username + "', '" + req.body.score + "')";
conn.query(queryStringInsert, function(err, rows, moreResultSets) {
if (err) {
res.json({ success: false });
}
else {
//var queryString = "SELECT Username, Score FROM Highscores ORDER BY Score DESC LIMIT 5";
var queryStringSelect = "SELECT Username, Score FROM Highscores ORDER BY Score DESC FETCH FIRST 5 ROWS ONLY";
conn.query(queryStringSelect, function(err, rows, moreResultSets) {
if (err) {
res.json({ success: false });
}
else {
conn.close(function() {
console.log(rows);
res.json({ data: rows, success: true });
});
}
});
}
});
}
});
});
// Start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
console.log("server starting on " + appEnv.url);
});