在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 第10章 增加pv統(tǒng)計和留言統(tǒng)計
第9章 增加標簽和標簽頁面
番外篇之——使用 Mongoose
番外篇之——使用 Async
第4章 實現用戶頁面和文章頁面
第12章 增加友情鏈接
第14章 增加頭像
第7章 實現分頁功能
第5章 增加編輯與刪除功能
第11章 增加文章檢索功能
第3章 增加文件上傳功能
番外篇之——部署到 Heroku
第2章 使用 Markdown
第13章 增加404頁面
第16章 增加日志功能
第1章 一個簡單的博客
番外篇之——使用 Handlebars
第10章 增加pv統(tǒng)計和留言統(tǒng)計
番外篇之——使用 Passport
第15章 增加轉載功能和轉載統(tǒng)計
第8章 增加存檔頁面
番外篇之——使用 generic pool
番外篇之——使用 _id 查詢
番外篇之——使用 Disqus
番外篇之——使用 KindEditor
第6章 實現留言功能

第10章 增加pv統(tǒng)計和留言統(tǒng)計

現在我們來給每篇文章增加 pv 統(tǒng)計和留言統(tǒng)計。

我們設定:在主頁、用戶頁和文章頁均顯示 pv 統(tǒng)計和留言統(tǒng)計。

修改 post.js ,將:

var post = {
    name: this.name,
    time: time,
    title:this.title,
    tags: this.tags,
    post: this.post,
    comments: []
};

修改為:

var post = {
    name: this.name,
    time: time,
    title:this.title,
    tags: this.tags,
    post: this.post,
    comments: [],
    pv: 0
};

注意:我們給要存儲的文檔添加了 pv 鍵并直接賦初值為 0。

打開 post.js ,將 Post.getOne() 修改為:

//獲取一篇文章
Post.getOne = function(name, day, title, callback) {
  //打開數據庫
  mongodb.open(function (err, db) {
    if (err) {
      return callback(err);
    }
    //讀取 posts 集合
    db.collection('posts', function (err, collection) {
      if (err) {
        mongodb.close();
        return callback(err);
      }
      //根據用戶名、發(fā)表日期及文章名進行查詢
      collection.findOne({
        "name": name,
        "time.day": day,
        "title": title
      }, function (err, doc) {
        if (err) {
          mongodb.close();
          return callback(err);
        }
        if (doc) {
          //每訪問 1 次,pv 值增加 1
          collection.update({
            "name": name,
            "time.day": day,
            "title": title
          }, {
            $inc: {"pv": 1}
          }, function (err) {
            mongodb.close();
            if (err) {
              return callback(err);
            }
          });
          //解析 markdown 為 html
          doc.post = markdown.toHTML(doc.post);
          doc.comments.forEach(function (comment) {
            comment.content = markdown.toHTML(comment.content);
          });
          callback(null, doc);//返回查詢的一篇文章
        }
      });
    });
  });
};

更多關于 $inc 的知識請參閱《mongodb權威指南》。

增加留言統(tǒng)計就簡單多了,直接取 comments.length 即可。修改 index.ejs 、user.ejs 及 article.ejs ,在:

<p><%- post.post %></p>

下一行添加一行代碼:

<p class="info">閱讀:<%= post.pv %> | 評論:<%= post.comments.length %></p>

現在,我們給博客增加了 pv 統(tǒng)計和留言統(tǒng)計。