emptymalei
2/24/2018 - 11:45 PM

nodejs + mongodb: fight the promise

nodejs + mongodb: fight the promise

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>BiliSpider</title>
    <link rel="stylesheet" href="assets/css/bulma.css">
    <link rel="stylesheet" href="assets/css/style.css">
  </head>
  
  <body>

      <section class="hero has-text-centered">
          <div class="hero-body has-text-centered">
            <div class="container">
              <h1 class="title is-1">
                BiliSpider
              </h1>
              
              <div class="notification text-centered" style="margin-bottom:2em;">
                <p style="margin-bottom:2em;">
                  一个用来获取 Bilibili 用户数据的爬虫
                </p>
                <pre style="min-height:1.25em;line-height: 1;" type="text" readonly>https://github.com/uupers/uupers.github.io</pre>
                
                  
                  
                </div>

                
                     <!-- BY UUPERS            -->
              
              
            </div>
            
            <div class="content">
                  <div class="text-centered">
                      <button class = "button is-dark is-centered" id = "btn-run">贡献你的计算力</button>
                  </div>
            </div>
          </div>
        </section>

<section>        
  <div id="log-pre">
      <span class="tag is-light">LOG:</span>
    <pre id="log-process">
      
    </pre>
    </div>
</section>

<section>


  <div>
      <span class="tag is-light">Database:</span>
    <div id="data-html">
        <pre id="db-info">
        </pre> 
    </div>
    </div>
</section>


        <script src = "./bilicrawler.js" ></script>
        <script src = "./assets/mongo.js" ></script>
    <script>
      // You can also require other files to run in this process
      require('./renderer.js')
    </script>
  </body>
</html>
const dbhtml = document.getElementById('db-info')

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');


// Connection URL
var url = 'mongodb://spiderrd:spiderrd@45.32.68.44:37017/bilibili_spider';

const dbName = 'bilibili_spider';
var db_size = 0;
var db_objects = 0 ;

Promise.all([
    MongoClient.connect(url, function(err, client) {
      assert.equal(null, err);
      console.log("Connected successfully to server");
      const db = client.db(dbName);
      db.command({'dbStats': 1}, function(err, results) {
          console.log(results);
          console.log( (results.dataSize)/1024/1024/1024);
          db_size = (results.dataSize)/1024/1024/1024;
          db_objects = results.objects;
      });
      client.close();
    }
    )]
).then(
  dbit(db_objects)
).then(
  dbit(db_size)
)


function dbit(elem) {
    dbhtml.innerHTML = elem;
}