Nodejs结合MongoDB
admin
2023-04-14 00:41:32
0

 var http = require("http"),
 mongo = require("mongodb"),
  events = require("events");
 http.createServer(function(req, res) {
   var products_emitter = new events.EventEmitter(),
      // 创建到northwind数据库的链接。相当于use northwind
      db = new mongo.Db("northwind", new mongo.Server('localhost', 27017, {}), {});
   var listener = function(products) {
      var html = [], len = products.length;
      html.push('');
      html.push('');
      html.push('');
      html.push('Nodejs');
      html.push('');
      html.push('');
      if(len > 0) {
        html.push('

    ');
            for(var i = 0; i < len; i++) {
              html.push('
  • ' + products[i].name + '
  • ');

            }
            html.push('
');
      }
      html.push('');
      html.push('');
       res.writeHead(200, "Content-Type: text/html");
      res.write(html.join(''));
      res.end();
       clearTimeout(timeout);
  }
  products_emitter.on('products', listener);
   var timeout = setTimeout(function() {
      products_emitter.emit('products', []);
      products_emitter.removeListener('products', listener);
  }, 10000);
   db.open(function() {
      // 打开名为products的表
    db.collection("products", function(err, collection) {
        // select * from products 相当于db.products.find()
      collection.find(function(err, cursor) {
        cursor.toArray(function(err, items) {
          products_emitter.emit('products', items);
        });
      });
    });
  });
 
}).listen(8000);
 console.log("Started");

相关内容

热门资讯

【第一消息】“福建天天开心到底... 有 亲,根据资深记者爆料福建天天开心是可以开挂的,确实有挂(咨询软件无需...
终于明白“闲来贵州麻将有没有挂... 终于明白“闲来贵州麻将有没有挂?”(太坑了果然有挂)您好,闲来贵州麻将这个游戏其实有挂的,确实是有挂...
最新引进“新皇豪炸/金/花究竟... 最新引进“新皇豪炸/金/花究竟有挂吗?”(详细开挂教程)您好,新皇豪炸/金/花这个游戏其实有挂的,确...
玩家攻略科普“微乐捉鸡麻将是不... 玩家攻略科普“微乐捉鸡麻将是不是有挂?”(太坑了原来有挂)您好,微乐捉鸡麻将这个游戏其实有挂的,确实...
重磅消息“马鞍山麻将到底有挂吗... 网上科普关于“马鞍山麻将有没有挂”话题很是火热,小编也是针对马鞍山麻将作*弊开挂的方法以及开挂对应的...
今日重大发现“科乐填大坑有没有... 网上科普关于“科乐填大坑有没有挂”话题很是火热,小编也是针对科乐填大坑作*弊开挂的方法以及开挂对应的...
【第一消息】“今日花牌是不是有... 家人们!今天小编来为大家解答今日花牌透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
华熠印刷取得彩盒储光过油印刷装... 国家知识产权局信息显示,浙江华熠印刷科技股份有限公司取得一项名为“彩盒储光过油印刷装置”的专利,授权...
今日重大通报“衣兆丰悦开挂神器... 网上科普关于“衣兆丰悦有没有挂”话题很是火热,小编也是针对衣兆丰悦作*弊开挂的方法以及开挂对应的知识...