node. JS Blog Instance (vi) Message function

Source: Internet
Author: User

The original tutorial Https://github.com/nswbmw/N-blog/wiki/_pages the fifth chapter, due to the version and other reasons, in the original tutorial based on a slight change can be achieved.

Realize the user to the message of the function of the message saved in the database.

Post.js, modify the document to be deposited in Post.prototype.save as:

Document to be stored in the database  var post = {      name:this.name,      time:time,      title:this.title,      post:this.post,  Comments:[]  };
We added a comments key (array) to the document to store the messages (objects) on this article. In order to also let the message support markdown syntax, we will post.getone in the function:
Doc.post = markdown.tohtml (doc.post); Modified to:
Parsing markdown as HTML
if (doc) {<span style= "White-space:pre" ></span>console.log ("444");d Oc.comments.forEach (function ( Comment) {comment.content = markdown.tohtml (comment.content);});}
blog/models/the new Comment.js file, add the following code:
var MongoDB = require ('./db '); function Comment (name, day, title, Comment) {this.name = Name;this.day = Day;this.title = Ti tle;this.comment = comment;} Module.exports = comment;//Store a message message Comment.prototype.save = function (callback) {var name = THIS.name, Day    = This.day ,    title = This.title,    comment = this.comment;//Open database Mongodb.open (function (err, db) {if (err) {return callback ( ERR);} Read posts Collection db.collection (' posts ', function (err, collection) {if (err) {mongodb.close (); return callback (ERR);} Find the document by user name, time and title, and add a message to the comments array of the document Collection.update ({"Name": Name, "Time.day": Day, "title": Title}, {$ Push: {"comments": Comment}, function (err) {mongodb.close (); if (err) {return callback (ERR);} callback (null);});};  
Modify the index.js in Post = require ('.. /models/post.js ') after adding a line of code:

Post=require ('.. /models/post.js '), Comment=require ('. /models/comment.js ');
Create the comment view file, and under the Blog/views/folder, add a new Comment.ejs, adding the following code:
&LT;BR/><% Post.comments.forEach (function (comment, index) {%> <p><a href= "<%= comment.website%& gt; "  ><%= comment.name%></a> <span class= "info" > Reply to <%= comment.time%></span></p> <p><%-comment.content%></p><%})%><form method= "POST" ><% if (user) {%> name: <in Put type= "text" name= "name" value= "<%= user.name%>"/><br/> mailbox: <input type= "text" name= "email" value= "<%= user.email%>"/><br/> URL: <input type= "text" name= "website" value= "/u/<%= user.name%>"/&G T;<br/><%} else {%> name: <input type= "text" name= "name"/><br/> mailbox: <input type= "Text" name= "Email"/><br/> URL: <input type= "text" name= "website" value= "/http"/><br/><%}%> <tex Tarea name= "Content" rows= "5" cols= "" ></textarea><br/> <input type= "Submit" value= "message"/></ Form>
Note: Depending on the user's login status, different prompt information is displayed here. Also note that the user is not logged in when the message, the URL this item needs to add http://prefix, or when the connection is generated based on the current URL (local is localhost:3000).
Open Article.ejs and add a line of code before <%-include footer%>:
</pre><pre name= "code" class= "HTML" ><%-include comment%>
So we introduced the message module on the article page.
Index.js Add code:

App.post ('/u/:name/:d ay/:title ', function (req, res) {var date = new Date (), time = Date.getfullyear () + "-" + (Date.getmon Th () + 1) + "-" + date.getdate () + "" + date.gethours () + ":" + (Date.getminutes () < 10 ") 0 ' + date.getminutes (): Date.getminutes ()), var comment = {name:req.body.name,email:req.body.email,website: Req.body.website,time:time,content:req.body.content};var newcomment = new Comment (Req.params.name, Req.params.day, Req.params.title, comment), Newcomment.save (function (err) {if (err) {Req.flash (' error ', err); return Res.redirect (' Back ');} Req.flash (' Success ', ' message success! '); Res.redirect (' Back ');});
Use Res.redirect (' back '); , that is, after the message has been successfully returned to the article page.
Look at the effect:

Go to the blog and log in and publish an article to test:


Click to enter the article can see the message section, to leave a statement:


Message success, click again to enter the article to view the message:


Note: After you modify the code, you can empty the contents of the Mongodb/blog folder before testing to avoid problems

node. JS Blog Instance (vi) Message function

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.