-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
62 lines (45 loc) · 1.71 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
var
gameport = process.env.PORT || 8008,//8008 main //6333 dev
//gameport = 'stick-battle.com',
express = require('express'),
http = require('http'),
app = express(),
server = http.createServer(app);
var version = 1.01;
server.listen(gameport);//gameport);//, 'stick-battle.com');
//set up mongodb
var dburl = '107.170.27.58/mongoapp';
var collections = ['highscores'];
var db = require('mongojs').connect(dburl, collections);
//db.users.remove({'username':'Rhys'});
db.highscores.ensureIndex({username:1} , {unique : true});
var highscore = function(username, score) {
this.username = username;
this.score = score;
}
var testUser = new highscore("Rhys",100);
db.highscores.save(testUser, function(err, savedUser) {
if(err || !savedUser) {
console.log("Failed saving a user. err: "+err);
}else
console.log("User successfully added. Name: "+ savedUser.username);
});
db.highscores.find(testUser, function(err, highscores) {
if(err || !highscores.length)
console.log("User "+ highscore.username +" not found.");
else highscores.forEach(function (username, score) {
console.log("User found! Username: "+username + " score: "+score);
});
});
console.log('\t Listening on port: ' + gameport );
//By default, we forward the / path to index.html automatically.
app.get('/', function (req, res) {
console.log('User loading page. Loading %s', __dirname + '/index.html')
res.sendfile( '/index.html' , { root:__dirname });
});
app.get( '/*' , function( req, res, next ) {
//This is the current file they have requested
var file = req.params[0];
//Send the requesting client the file.
res.sendfile( __dirname + '/' + file );
}); //app.get