A simple blog system (14) Add user avatar

Source: Internet
Author: User

1. Now let's add the avatar to your blog.

Assume: Registered users according to the Mailbox Lake area Gravatar Avatar, unregistered users according to the message filled out the mailbox to get Gravatar avatar, on the homepage and the user also on the right side of the title of the article to show the author's avatar, the article page message person's avatar displayed on the right side of the message board.

1.1 Add already registered user picture

We have introduced the Crypto encryption module, which has already been referenced, this way can be used directly, first, open user.js, we add a line of code:

var crypto = require (' crypto ');

We then modify the code inside the User.prototype.save () to resemble the following:

varMD5 = Crypto.createhash (' MD5 ')), Email_md5= Md5.update ( This. Email.tolowercase ()). Digest (' Hex '),//Convert email to lowercase in encodingHead = "http://www.gravatar.com/avatar/" + Email_md5 + "? s=48";//added head avatar link key for later use    //documents to be stored in the database    varuser = {//User InformationName This. Name, Password: This. Password, email: This. email, head:head};

Next, open the Index.js and modify the code as follows:

Router.post ('/post ',function(req, res) {varCurrentUser =Req.session.user, tags=[Req.body.tag1, Req.body.tag2], post=NewPost (Currentuser.name, Currentuser.head, req.body.title, tags, req.body.post); Post.save (function(err) {if(Err) {Req.flash (' Error ', err); returnRes.redirect ('/'); } req.flash (' Success ', ' released successfully! ‘); Res.redirect (‘/‘);//Post success jump to homepage    });});

Then we open the post.js and make the following changes:

 function   post (name, head, title, tags, Post    { this . Name = name;      head;     this . Title = title;     this . Tags = tags;   post;}  
var post = {        this. Name,        this. Head,        Time:time,         this.title,        the This. tags,                comments: [],         0    // Pass in PV value and assign it to 0    };

Finally, we open User.ejs and Index.ejs and add the following code after the H4 tag:

<a href= "/u/<%= post.name%>" ></a>

2. Add an avatar for an unregistered user:

Open Index.js and modify the code shown below:

//message Corresponding pageRouter.post ('/u/:name/:d ay/:title ',function(req, res) {varDate =NewDate (), time= Date.getfullyear () + "-" + (Date.getmonth () + 1) + "-" + date.getdate () + "" +date.gethours ()+ ":" + (Date.getminutes () < 10? ' 0 ' +date.getminutes (): Date.getminutes ()); varMD5 = Crypto.createhash (' MD5 ')), Email_md5= Md5.update (Req.body.email.toLowerCase ()). Digest (' Hex '),//Convert email to lowercase in encodingHead = "http://www.gravatar.com/avatar/" + Email_md5 + "? s=48";//added head avatar link key for later use      varComment ={name:req.body.name, Head:head, Email:req.body.email, WEBSITE:REQ.BODY.WEBSI      Te, Time:time, content:req.body.content}; varNewComment =NewComment (Req.params.name, Req.params.day, Req.params.title, Comment); Newcomment.save (function(err) {if(Err) {Req.flash (' Error ', err); returnRes.redirect (' Back ')); } req.flash (' Success ', ' message success! '); Res.redirect (' Back '); });
});

Then open Comment.ejs and modify the code as follows:

<p>        box-shadow:0px 1px 4px #888;" />    <a href= "<%= comment.website%>" ><%= comment.name%></a>    <span> reply to <% = Comment.time%></span></p>

At this point, we have added a registered user and unregistered user's avatar display function to the blog.

A simple blog system (14) Add user avatar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.