Advertisement
zzarbi

Node.js/Mongo

Feb 2nd, 2012
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var sys = require('sys'),
  2. http = require('http'),
  3. mongodb = require('mongodb');
  4.  
  5. http.createServer(function(req, res) {
  6.     var server = new mongodb.Server("127.0.0.1", 27017, {});
  7.     var db = new mongodb.Db('mongo', server, {});
  8.    
  9.     db.open(function (error, client) {
  10.         if (error) {
  11.             db.close();
  12.             throw error;
  13.         }
  14.        
  15.         var collection = new mongodb.Collection(client, 'basic');
  16.        
  17.         collection.find({'_id':String(Math.floor(Math.random()*100000))}, {}, function(err, cursor) {
  18.             if(err == null){
  19.              cursor.toArray(function(err, docs){
  20.                  db.close();
  21.                  res.writeHead(200, {'Content-Type': 'text/html'});
  22.                      res.write('Goood');
  23.                  res.end();
  24.              });
  25.             }else{
  26.                 db.close();
  27.                 res.writeHead(200, {'Content-Type': 'text/html'});
  28.                 res.write('Bad');
  29.                 res.end();
  30.                 throw err;
  31.             }
  32.         });
  33.     });
  34. }).listen(8080);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement