Node. js blog instance (6) Message Function
Original tutorial.
This function enables users to leave messages for articles. The messages are stored in the database.
Post. js, modify the document to be saved in Post. prototype. save as follows:
// Var post = {name: this. name, time: time, title: this. title, post: this. post, comments: []};The comments key (array) is added to the document to store messages (objects) in this article ). To enable the message to support the markdown syntax, we set the following in the Post. getOne function:
Doc. post = markdown. toHTML (doc. post); changed:
// Parse markdown as html
if (doc) {doc.comments.forEach(function (comment) {comment.content = markdown.toHTML(comment.content);});}Create a comment. js file under blog/models/and add the following code:
Var mongodb = require ('. /db'); function Comment (name, day, title, comment) {this. name = name; this. day = day; this. title = title; this. comment = comment;} module. exports = Comment; // stores Comment messages. prototype. save = function (callback) {var name = this. name, day = this. day, title = this. title, comment = this. comment; // open the database mongodb. open (function (err, db) {if (err) {return callback (err);} // read the posts set db. collection ('posts', function (err, collection) {if (err) {mongodb. close (); return callback (err);} // search for a document by user name, time, and title, and add a message object to the comments array of the document. update ({"name": name, "time. day ": day," title ": title}, {$ push: {" comments ": comment }}, function (err) {mongodb. close (); if (err) {return callback (err) ;}callback (null );});});});};Modify index. js and add a line of code after Post = require ('../models/post. js:
Post=require('../models/post.js'), Comment=require('../models/comment.js');Create a comment View File, create comment. ejs in the blog/views/folder, and add the following code:
<% Post. comments. forEach (function (comment, index) {%>"> <% = Comment. name %> reply to <% = comment. time %>
<%-Comment. content %>
<%}) %>
Note: Different prompts are displayed based on the user logon status. Note that when an Unlogged-on user leaves a message, the website must be prefixed with http, otherwise, the connection is generated based on the current url (localhost: 3000 ).
Open article. ejs and add a line of code before <%-include footer %>:
<%- include comment %>
In this way, the message module is introduced on the article page.
Add code for index. js:
App. post ('/u/: name/: day/: title', function (req, res) {var date = new Date (), time = date. getFullYear () + "-" + (date. getMonth () + 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 successful! '); Res. redirect ('back ');});});Use res. redirect ('back'); to return to this article page after the message is successfully sent.
See the results:
Log on to your blog and post an article for testing:
VcH00dSw5b/po6zAtMH0uPbR1KO6PC9wPgo8cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140828/20140828084916329.png" alt = "\">
The message is sent successfully. Click again to enter the article to view the message:
Note: Before testing, you can clear the content in the mongodb/blog folder to avoid problems.