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

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

第14章 增加頭像

現(xiàn)在我們來(lái)給博客添加用戶頭像,注冊(cè)的用戶根據(jù)注冊(cè)時(shí)的郵箱獲取 gravatar 頭像,未注冊(cè)的用戶則根據(jù)留言填的郵箱獲取 gravatar 頭像。 什么是 gravatar ?詳情請(qǐng)戳:http://www.gravatar.com

我們?cè)O(shè)定:在主頁(yè)和用戶頁(yè)的文章標(biāo)題右側(cè)顯示作者頭像,在文章頁(yè)面留言的人的頭像顯示在留言板左側(cè)。

我們需要用到 Node.js 中的 crypto 模塊,之前已經(jīng)引入過(guò),所以這里可以直接使用。

添加已注冊(cè)用戶的頭像

打開 user.js ,在最上面添加一行代碼:

var crypto = require('crypto');

然后將 User.prototype.save 內(nèi)的:

var user = {
    name: this.name,
    password: this.password,
    email: this.email
};

修改為:

var md5 = crypto.createHash('md5'),
    email_MD5 = md5.update(this.email.toLowerCase()).digest('hex'),
    head = "http://www.gravatar.com/avatar/" + email_MD5 + "?s=48";
//要存入數(shù)據(jù)庫(kù)的用戶信息文檔
var user = {
    name: this.name,
    password: this.password,
    email: this.email,
    head: head
};

這里我們?cè)谟脩粑臋n里添加了 head 鍵,方便后面使用。

注意:需要把 email 轉(zhuǎn)化成小寫再編碼。

打開 index.js ,將 app.post('/post') 中的:

post = new Post(currentUser.name, req.body.title, tags, req.body.post);

修改成:

post = new Post(currentUser.name, currentUser.head, req.body.title, tags, req.body.post);

修改 post.js ,將:

function Post(name, title, tags, post) {
  this.name = name;
  this.title = title;
  this.tags = tags;
  this.post = post;
}

修改為:

function Post(name, head, title, tags, post) {
  this.name = name;
  this.head = head;
  this.title = title;
  this.tags = tags;
  this.post = post;
}

將:

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

修改為:

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

最后,修改 index.ejs 和 user.ejs ,在 后添加一行代碼:

<a href="/u/<%= post.name %>"><img src="<%= post.head %>" class="r_head" /></a>

至此,我們實(shí)現(xiàn)了給已注冊(cè)的用戶添加頭像的功能。

添加未注冊(cè)用戶的頭像

修改 app.post('/u/:name/:day/:title'),將:

var comment = {
    name: req.body.name,
    email: req.body.email,
    website: req.body.website,
    time: time,
    content: req.body.content
};

修改為:

var md5 = crypto.createHash('md5'),
    email_MD5 = md5.update(req.body.email.toLowerCase()).digest('hex'),
    head = "http://www.gravatar.com/avatar/" + email_MD5 + "?s=48"; 
var comment = {
    name: req.body.name,
    head: head,
    email: req.body.email,
    website: req.body.website,
    time: time,
    content: req.body.content
};

打開 comment.ejs ,將:

<% post.comments.forEach(function (comment, index) { %>
  <p><a href="<%= comment.website %>"><%= comment.name %></a>
  <span class="info"> 回復(fù)于 <%= comment.time %></span></p>
  <p><%- comment.content %></p>
<% }) %>

修改為:

<% post.comments.forEach(function (comment, index) { %>
  <div style="padding-left:4em">
    <p><img src="<%= comment.head %>" class="l_head" /><a href="<%= comment.website %>"><%= comment.name %></a>
    <span class="info"> 回復(fù)于 <%= comment.time %></span></p>
    <p><%- comment.content %></p>
  </div>
<% }) %>

最后,在 style.css 中添加兩行樣式:

.l_head{float:left;margin-left:-4em;box-shadow:0px 1px 4px #888;}
.r_head{float:right;margin-top:-2.5em;box-shadow:0px 1px 4px #888;}

現(xiàn)在,我們給博客添加了頭像的功能。