-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
195 lines (167 loc) · 5.4 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/env node
var express = require("express");
var fs = require("fs");
var mongodb = require("mongodb");
var App = function () {
// Scope
var self = this;
// Setup
self.dbServer = new mongodb.Server(process.env.OPENSHIFT_MONGODB_DB_HOST, parseInt(process.env.OPENSHIFT_MONGODB_DB_PORT), {
safe: true
});
// self.db = new mongodb.Db(process.env.OPENSHIFT_APP_NAME, self.dbServer, {auto_reconnect: true});
self.dbServer = new mongodb.Server(process.env.OPENSHIFT_MONGODB_DB_HOST, parseInt(process.env.OPENSHIFT_MONGODB_DB_PORT));
self.db = new mongodb.Db(process.env.OPENSHIFT_APP_NAME, self.dbServer, {
safe: true
});
//self.db = monk("mongodb://"+process.env.OPENSHIFT_MONGODB_DB_HOST+":"+process.env.OPENSHIFT_MONGODB_DB_PORT+"/");
self.dbUser = process.env.OPENSHIFT_MONGODB_DB_USERNAME;
self.dbPass = process.env.OPENSHIFT_MONGODB_DB_PASSWORD;
self.ipaddr = process.env.OPENSHIFT_NODEJS_IP;
self.port = parseInt(process.env.OPENSHIFT_NODEJS_PORT) || 8080;
if (typeof self.ipaddr === "undefined") {
console.warn("No OPENSHIFT_NODEJS_IP environment variable");
};
// Web app logic
self.routes = {};
// Web app urls
self.app = express();
//This uses the Connect frameworks body parser to parse the body of the post request
var methodOverride = require("method-override");
// parse application/x-www-form-urlencoded
//self.app.use(bodyParser.urlencoded());
var bodyParser = require("body-parser");
// parse application/json
self.app.use(bodyParser.json());
self.app.use(bodyParser.urlencoded({
extended: true
}));
// override with POST having ?_method=DELETE
self.app.use(methodOverride("_method"));
//define all the custom url map functio ns
//default response with info about app URLs
self.app.get("/", function (req, res) {
res.send("root response~");
});
self.app.post("/addtime", function (req, res) {
//app.connectDb(function callback(){})
// Get our form values
var userName = req.body["UserName"];
var userID = req.body["UserID"];
var levelName = req.body["LevelName"];
var Time = req.body["Time"];
var check = req.body["Check"];
var mycheck = Math.tan(Math.cos(Time));
// Check if the check"s correct
if (Math.abs(check - mycheck) > 0.05) {
console.log("Stuff went wrong; check what's wrong.");
res.write("Something went wrong I guess, sorry.");
return
};
// Set our collection
//collection.save(
//{"UserID" : -22, "LevelName" : "TestLevel1", "Time" : 90001, "UserName" : "Guest 15"}
//self.db.collection("AllTimes").insert({"UserID" : -123, "LevelName" : "LevelTest1", "Time" : 90000, "UserName" : "Guest 123"}, function (err, result){ if (err) throw err; console.log(result);});
//var collection = self.db.get("AllTimes");
var lookFor = {
"UserID": userID,
"LevelName": levelName
}
var toPush = {
"UserID": userID,
"LevelName": levelName,
"Time": Time,
"UserName": userName
}
self.db.collection("AllTimes")
.update(
lookFor, toPush, {
upsert: true
},
function updcallback(err, result) {
if (err) throw err;
console.log(result);
}
);
res.end();
return
});
self.app.get("/gettimes", function (req, res) {
//app.connectDb(function callback(){})
var reqminRank = req.query.minRank;
var reqmaxRank = req.query.maxRank; //size of table
var reqminTime = req.query.minTime; //sort by time~
var reqmaxTime = req.query.maxTime;
var reqUserID = req.query.UserID;
var timestrang = "Times";
var reqLevelName = req.param("LevelName");
//var collection = self.db.get("AllTimes");
//self.db.collection("AllTimes").find(
self.db.collection("AllTimes")
.find({
"LevelName": reqLevelName
})
.sort({
"Time": 1
})
.toArray(function (err, names) {
if (err) {
throw err
};
res.header("Content-Type:", "application/json");
res.end(JSON.stringify(names));
});
});
/*,
function(err, cursor){
if (cursor){
cursor.sort("Time", -1);
res.write(JSON.stringify(cursor)); //converting circular structure to JSON
};
res.end();
}); */
//});
// Logic to open a database connection. We are going to call this outside of app so it is available to all our functions inside.
self.connectDb = function (callback) {
self.db.open(function (err, db) {
if (err) {
throw err
};
self.db.authenticate(self.dbUser, self.dbPass, {
authdb: "admin"
}, function (err, res) {
if (err) {
throw err
};
callback();
});
});
};
//starting the nodejs server with express
self.startServer = function () {
self.app.listen(self.port, self.ipaddr, function () {
console.log("%s: Node server started on %s:%d ...", Date(Date.now()), self.ipaddr, self.port);
});
};
// Destructors
self.terminator = function (sig) {
if (typeof sig === "string") {
console.log("%s: Received %s - terminating Node server ...", Date(Date.now()), sig);
process.exit(1);
};
console.log("%s: Node server stopped.", Date(Date.now()));
};
process.on("exit", function () {
self.terminator();
});
self.terminatorSetup = function (element, index, array) {
process.on(element, function () {
self.terminator(element);
});
};
["SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGTERM"].forEach(self.terminatorSetup);
};
//make a new express app
var app = new App();
//call the connectDb function and pass in the start server command
app.connectDb(app.startServer);