dileephell

Untitled

Jan 23rd, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express')
  2. , http = require('http') , path = require('path');
  3.  
  4. var app = express();
  5.  
  6. // all environments
  7. app.set('port', process.env.PORT || 5000);
  8. app.set('views', __dirname + '/views');
  9. app.set('view engine', 'jade');
  10. app.use(express.favicon());
  11. app.use(express.logger('dev'));
  12. app.use(express.bodyParser());
  13. app.use(express.methodOverride());
  14. app.use(app.router);
  15. app.use(express.static(path.join(__dirname, 'public')));
  16.  
  17. // development only
  18. if ('development' == app.get('env'))
  19. {
  20. app.use(express.errorHandler());
  21. }
  22.  
  23. // Connect DB
  24. var dbName = 'MasterData';
  25. var collections = ['games'];
  26.  
  27. var db = require('mongojs').connect(dbName, collections);
  28.  
  29. // Setup Routes
  30. app.get('/', function(req, res)
  31. {
  32. res.render('index', {title: "TATA"});
  33. });
  34.  
  35. app.get('/games', function(req, res) {
  36. db.games.find({}, function(err, games) {
  37. if (err) { console.log("no games"); }
  38. res.json(games);
  39. });
  40. });
  41.  
  42. app.get('/games/:id', function(req, res) {
  43. var id = req.params.id;
  44. db.games.find({ "_id" : db.ObjectId(id) }, function(err, games) {
  45. if (err) { throw err }
  46. res.json(games);
  47. });
  48. });
  49.  
  50. // POST
  51. app.post('/games', function(req, res) {
  52. res.json(req.body);
  53. db.games.save(req.body);
  54. });
  55.  
  56.  
  57.  
  58.  
  59. http.createServer(app).listen(app.get('port'), function(){
  60. console.log('Express server listening on port ' + app.get('port'));
  61. });
  62.  
  63.  
  64.  
  65. the json that i stored in MasterData db using games collection is
  66.  
  67.  
  68. [
  69. {
  70. "0": {
  71. "id":" 1234",
  72. "appid": " 12345678909h1230",
  73. "name": "mark",
  74. "gameid": "123456789098g123",
  75. "os": "Ipad"
  76. },
  77. "1": {
  78. "id":" 5678",
  79. "appid": " 43215678909h1230",
  80. "name": "james",
  81. "gameid": "123456789098g123",
  82. "os": "Ipad"
  83. },
  84. "2": {
  85. "id":" 9011",
  86. "appid": " 98015678909h1230",
  87. "name": "amgela",
  88. "gameid": "123456789098g123",
  89. "os": "Ipad"
  90. },
  91. "3": {
  92. "id":" 1213",
  93. "appid": " 45675678909h1230",
  94. "name": "gimmy",
  95. "gameid": "123456789098g123",
  96. "os": "Ipad"
  97. },
  98. "4": {
  99. "id":" 1415",
  100. "appid": " 00985678909h1230",
  101. "name": "jaine",
  102. "gameid": "123456789098g123",
  103. "os": "Ipad"
  104. },
  105. "_id": "52e0c7ade4b099eb8ffc8487"
  106. }
  107. ]
Advertisement
Add Comment
Please, Sign In to add comment