Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.js
- var express = require('express');
- var app = express();
- var path = require('path');
- var http = require('http');
- var io = require('socket.io');
- var game = require('./routes/games');
- app.configure(function () {
- app.set('port', process.env.PORT || 3000);
- //app.set('view engine', 'html');
- //app.set('views', './views');
- app.use(express.favicon());
- app.use(express.logger('dev'));
- app.use(express.bodyParser());
- app.use(express.cookieParser('lambent'));
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(express.session());
- app.use(express.static(path.join(__dirname, 'public')));
- app.use(express.errorHandler());
- app.use(function(req, res, next) {
- console.log('Request from: ' + req.ip);
- next();
- });
- });
- var server = http.createServer(app);
- io = io.listen(server);
- io.configure(function () {
- io.set('authorization', function (handshakeData, callback) {
- if (handshakeData.xdomain) {
- callback('Cross-domain connections are not allowed');
- } else {
- callback(null, true);
- }
- });
- });
- server.listen(app.get('port'), function () {
- console.log("Express server listening on port " + app.get('port'));
- });
- app.get('/',function(req,res)
- {
- res.send('index',{title: "TATA"});
- });
- app.get('/games', game.findAll);
- app.get('/games/:id', game.findById);
- app.post('/games', game.addGame);
- app.put('/games/:id', game.updateGame);
- app.delete('/games/:id', game.deleteGame);
- io.sockets.on('connection', function (socket) {
- socket.on('message', function (message) {
- console.log("Got message: " + message);
- ip = socket.handshake.address.address;
- url = message;
- 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()});
- });
- socket.on('disconnect', function () {
- console.log("Socket disconnected");
- io.sockets.emit('pageview', { 'connections': Object.keys(io.connected).length});
- });
- });
- games.js
- var mongo = require('mongodb');
- var Server = mongo.Server,
- Db = mongo.Db,
- BSON = mongo.BSONPure;
- var server = new Server('localhost', 27017, {auto_reconnect: true});
- db = new Db('gamedb', server, {safe: true});
- db.open(function(err, db) {
- if(!err) {
- console.log("Connected to 'gamedb' database");
- db.collection('games', {safe:true}, function(err, collection) {
- if (err) {
- console.log("The 'games' collection doesn't exist. Creating it with sample data...");
- populateDB();
- }
- });
- }
- });
- exports.findById = function(req, res) {
- var id = req.params.id;
- console.log('Retrieving game: ' + id);
- db.collection('games', function(err, collection) {
- collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, game) {
- res.send(game);
- });
- });
- };
- exports.findAll = function(req, res) {
- //db.collection('games', function(err, collection) {
- //collection.find().toArray(function(err, items) {
- res.send('<h1>Welcome to Games!</h1>');
- //});
- //});
- };
- exports.addGame = function(req, res) {
- var game = req.body;
- console.log('Adding game: ' + JSON.stringify(game));
- db.collection('games', function(err, collection) {
- collection.insert(game, {safe:true}, function(err, result) {
- if (err) {
- res.send({'error':'An error has occurred'});
- } else {
- console.log('Success: ' + JSON.stringify(result[0]));
- res.send(result[0]);
- }
- });
- });
- }
- exports.updateGame = function(req, res) {
- var id = req.params.id;
- var game = req.body;
- delete game._id;
- console.log('Updating wine: ' + id);
- console.log(JSON.stringify(game));
- db.collection('games', function(err, collection) {
- collection.update({'_id':new BSON.ObjectID(id)}, game, {safe:true}, function(err, result) {
- if (err) {
- console.log('Error updating game: ' + err);
- res.send({'error':'An error has occurred'});
- } else {
- console.log('' + result + ' document(s) updated');
- res.send(game);
- }
- });
- });
- }
- exports.deleteGame = function(req, res) {
- var id = req.params.id;
- console.log('Deleting game: ' + id);
- db.collection('games', function(err, collection) {
- collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
- if (err) {
- res.send({'error':'An error has occurred - ' + err});
- } else {
- console.log('' + result + ' document(s) deleted');
- res.send(req.body);
- }
- });
- });
- }
- /*--------------------------------------------------------------------------------------------------------------------*/
- // Populate database with sample data -- Only used once: the first time the application is started.
- var populateDB = function() {
- var games = [
- { appid:"air123",
- name: "AirSurfer",
- year: "2014",
- country: "INDIA",
- region: "NEW DELHI",
- description: "DASH as fast as you can! .",
- picture: ""
- },
- {
- name: "AirSurfer",
- year: "2014",
- country: "INDIA",
- region: "NEW DELHI",
- description: "DASH as fast as you can! .",
- picture: ""
- },
- {
- name: "AirSurfer",
- year: "2014",
- country: "INDIA",
- region: "NEW DELHI",
- description: "DASH as fast as you can! .",
- picture: ""
- },
- {
- name: "AirSurfer",
- year: "2014",
- country: "INDIA",
- region: "NEW DELHI",
- description: "DASH as fast as you can! .",
- picture: ""
- }];
- db.collection('games', function(err, collection) {
- collection.insert(games, {safe:true}, function(err, result) {});
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment