Nodejs结合MongoDB-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Nodejs结合MongoDB

 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");

创新互联公司主要从事网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务密云,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792


本文标题:Nodejs结合MongoDB
地址分享:http://kswsj.cn/article/iegjpd.html

其他资讯