dileephell

Untitled

Jan 27th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. app.js
  2.  
  3. var express = require('express');
  4. var app = express();
  5. var path = require('path');
  6. var http = require('http');
  7. var io = require('socket.io');
  8. var game = require('./routes/games');
  9.  
  10.  
  11. app.configure(function () {
  12. app.set('port', process.env.PORT || 3000);
  13. //app.set('view engine', 'html');
  14. //app.set('views', './views');
  15. app.use(express.favicon());
  16. app.use(express.logger('dev'));
  17. app.use(express.bodyParser());
  18. app.use(express.cookieParser('lambent'));
  19. app.use(express.methodOverride());
  20. app.use(app.router);
  21. app.use(express.session());
  22. app.use(express.static(path.join(__dirname, 'public')));
  23. app.use(express.errorHandler());
  24.  
  25. app.use(function(req, res, next) {
  26. console.log('Request from: ' + req.ip);
  27. next();
  28. });
  29. });
  30.  
  31. var server = http.createServer(app);
  32. io = io.listen(server);
  33.  
  34.  
  35. io.configure(function () {
  36. io.set('authorization', function (handshakeData, callback) {
  37. if (handshakeData.xdomain) {
  38. callback('Cross-domain connections are not allowed');
  39. } else {
  40. callback(null, true);
  41. }
  42. });
  43. });
  44.  
  45. server.listen(app.get('port'), function () {
  46. console.log("Express server listening on port " + app.get('port'));
  47. });
  48.  
  49. app.get('/',function(req,res)
  50. {
  51. res.send('index',{title: "TATA"});
  52. });
  53.  
  54. app.get('/games', game.findAll);
  55. app.get('/games/:id', game.findById);
  56. app.post('/games', game.addGame);
  57. app.put('/games/:id', game.updateGame);
  58. app.delete('/games/:id', game.deleteGame);
  59.  
  60. io.sockets.on('connection', function (socket) {
  61.  
  62. socket.on('message', function (message) {
  63. console.log("Got message: " + message);
  64. ip = socket.handshake.address.address;
  65. url = message;
  66. io.sockets.emit('pageview', { 'connections': Object.keys(io.connected).length, 'ip': '***.***.***.' + ip.substring(ip.lastIndexOf('.') + 1), 'url': url, 'xdomain': socket.handshake.xdomain, 'timestamp': new Date()});
  67. });
  68.  
  69. socket.on('disconnect', function () {
  70. console.log("Socket disconnected");
  71. io.sockets.emit('pageview', { 'connections': Object.keys(io.connected).length});
  72. });
  73.  
  74. });
  75.  
  76.  
  77.  
  78. games.js
  79.  
  80.  
  81. var mongo = require('mongodb');
  82.  
  83. var Server = mongo.Server,
  84. Db = mongo.Db,
  85. BSON = mongo.BSONPure;
  86.  
  87. var server = new Server('localhost', 27017, {auto_reconnect: true});
  88. db = new Db('gamedb', server, {safe: true});
  89.  
  90. db.open(function(err, db) {
  91. if(!err) {
  92. console.log("Connected to 'gamedb' database");
  93. db.collection('games', {safe:true}, function(err, collection) {
  94. if (err) {
  95. console.log("The 'games' collection doesn't exist. Creating it with sample data...");
  96. populateDB();
  97. }
  98. });
  99. }
  100. });
  101.  
  102. exports.findById = function(req, res) {
  103. var id = req.params.id;
  104. console.log('Retrieving game: ' + id);
  105. db.collection('games', function(err, collection) {
  106. collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, game) {
  107. res.send(game);
  108. });
  109. });
  110. };
  111.  
  112. exports.findAll = function(req, res) {
  113. //db.collection('games', function(err, collection) {
  114. //collection.find().toArray(function(err, items) {
  115. res.send('<h1>Welcome to Games!</h1>');
  116. //});
  117. //});
  118. };
  119.  
  120. exports.addGame = function(req, res) {
  121. var game = req.body;
  122. console.log('Adding game: ' + JSON.stringify(game));
  123. db.collection('games', function(err, collection) {
  124. collection.insert(game, {safe:true}, function(err, result) {
  125. if (err) {
  126. res.send({'error':'An error has occurred'});
  127. } else {
  128. console.log('Success: ' + JSON.stringify(result[0]));
  129. res.send(result[0]);
  130. }
  131. });
  132. });
  133. }
  134.  
  135. exports.updateGame = function(req, res) {
  136. var id = req.params.id;
  137. var game = req.body;
  138. delete game._id;
  139. console.log('Updating wine: ' + id);
  140. console.log(JSON.stringify(game));
  141. db.collection('games', function(err, collection) {
  142. collection.update({'_id':new BSON.ObjectID(id)}, game, {safe:true}, function(err, result) {
  143. if (err) {
  144. console.log('Error updating game: ' + err);
  145. res.send({'error':'An error has occurred'});
  146. } else {
  147. console.log('' + result + ' document(s) updated');
  148. res.send(game);
  149. }
  150. });
  151. });
  152. }
  153.  
  154. exports.deleteGame = function(req, res) {
  155. var id = req.params.id;
  156. console.log('Deleting game: ' + id);
  157. db.collection('games', function(err, collection) {
  158. collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
  159. if (err) {
  160. res.send({'error':'An error has occurred - ' + err});
  161. } else {
  162. console.log('' + result + ' document(s) deleted');
  163. res.send(req.body);
  164. }
  165. });
  166. });
  167. }
  168. /*--------------------------------------------------------------------------------------------------------------------*/
  169. // Populate database with sample data -- Only used once: the first time the application is started.
  170.  
  171. var populateDB = function() {
  172.  
  173. var games = [
  174.  
  175. { appid:"air123",
  176. name: "AirSurfer",
  177. year: "2014",
  178. country: "INDIA",
  179. region: "NEW DELHI",
  180. description: "DASH as fast as you can! .",
  181. picture: ""
  182. },
  183. {
  184. name: "AirSurfer",
  185. year: "2014",
  186. country: "INDIA",
  187. region: "NEW DELHI",
  188. description: "DASH as fast as you can! .",
  189. picture: ""
  190. },
  191. {
  192. name: "AirSurfer",
  193. year: "2014",
  194. country: "INDIA",
  195. region: "NEW DELHI",
  196. description: "DASH as fast as you can! .",
  197. picture: ""
  198. },
  199. {
  200. name: "AirSurfer",
  201. year: "2014",
  202. country: "INDIA",
  203. region: "NEW DELHI",
  204. description: "DASH as fast as you can! .",
  205. picture: ""
  206. }];
  207.  
  208. db.collection('games', function(err, collection) {
  209. collection.insert(games, {safe:true}, function(err, result) {});
  210. });
  211.  
  212. };
Advertisement
Add Comment
Please, Sign In to add comment