node. JS Blog Instance (v) Edit and delete features

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.

Now add the ability to edit articles and delete articles to your blog.

When a user is online, only allow him to edit or delete his published article page, editing, can only edit the article content, can not edit the article title.

In Style.css, add the following style:

. edit{margin:3px;padding:2px 5px;border-radius:3px;background-color: #f3f3f3; color: #333; font-size:13px;}. Edit:hover{text-decoration:none;background-color: #f00; color: #fff;-webkit-transition:color. 2s linear;}

By detecting whether the user name exists in the session, and if it exists and is the same as the author name of the current article page, the Edit and delete buttons are displayed or not:

Article.ejs, modify the code to read as follows:

<%-include header%><p>  <% if (user && (User.Name = = Post.name)) {%>  <span>< A class= "edit" href= "/edit/<%= post.name%>/<%= post.time.day%>/<%= post.title%>" > Edit </a> </span>  <span><a class= "edit" href= "/remove/<%= post.name%>/<%= post.time.day%>/ <%= post.title%> "> Delete </a></span><%}%></p><p class=" info ">  <a href="/ u/<%= post.name%> "><%= post.name%></a> |   Date: <%= post.time.minute%></p><p><%-post.post%></p><%-include footer%>
Post.js, add the following code:
Returns the original published content (markdown format) Post.edit = function (name, day, title, callback) {//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);} Query based on user name, publication date and article name Collection.findone ({"Name": Name, "Time.day": Day, "title": Title}, function (Err, doc) { Mongodb.close (); if (err) {return callback (ERR);}  Callback (null, DOC);//return query for an article (markdown format)});}); });};/ /Update an article and related information post.update = function (name, day, title, post, callback) {//Open database Mongodb.open (function (err, db) {if (err) {R Eturn callback (ERR);} Read posts Collection db.collection (' posts ', function (err, collection) {if (err) {mongodb.close (); return callback (ERR);} Update article content collection.update ({"Name": Name, "Time.day": Day, "title": Title}, {$set: {post:post}}, function (err) { Mongodb.close (); if (err) {return callback (ERR);} callback (null);});});};/ /delete an article Post.remove = function (name, day, title, callback) {//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 and delete an article based on user name, date, and title Collection.remove ({"Name": Name, "Time.day": Day, "title": Title}, {w:1}, function (err) { Mongodb.close (); if (err) {return callback (ERR);} callback (null);});};
Index.js, add the following code:
<span style= "White-space:pre" ></span>app.get ('/edit/:name/:d ay/:title ', checklogin); App.get ('/edit/: name/:d ay/:title ', function (req, res) {var currentUser = Req.session.user; Post.edit (Currentuser.name, req.params.day, Req.params.title, function (err, POST) {if (err) {Req.flash (' error ', err); Return Res.redirect (' Back ');} Res.render (' edit ', {title: ' Editor ', Post:post,user:req.session.user,success:req.flash (' success '). ToString (), Error: Req.flash (' Error '). toString ()});}); App.post ('/edit/:name/:d ay/:title ', checklogin); App.post ('/edit/:name/:d ay/:title ', function (req, res) {var CurrentUser = Req.session.user; Post.update (Currentuser.name, Req.params.day, Req.params.title, req.body.post, function (err) {var url = '/u/' + Req.para Ms.name + '/' + req.params.day + '/' + req.params.title;if (err) {Req.flash (' error ', err); return res.redirect (URL);//Error! Back to the article page}req.flash (' Success ', ' modified successfully! '); Res.redirect (URL);//success! Return to the article page}); App.get ('/remove/:name/:d ay/:title ', checklogin); App.get ('/remove/:name/:d ay/:title ', function (req, res) {var currentUser = Req.session.user; Post.remove (Currentuser.name, req.params.day, Req.params.title, function (err) {if (err) {Req.flash (' error ', err); Return Res.redirect (' Back ');} Req.flash (' Success ', ' delete succeeded! '); Res.redirect ('/');});
To create a new Edit.ejs under blog/views/, add the following code:
<%-include header%><form method= "POST" >  title: <br/> <input  type= "text" name= "title" Value = "<%= post.title%>" disabled= "disabled"/><br/>  body: <br/> <textarea  name= "POST" rows = "cols=" ><%= post.post%></textarea><br/> <input  type= "Submit" value= "Save Changes"/ ></form><%-include footer%>
Finished! Try the effect, log in first, and then test the edit and delete operations:

Click to enter the article to see the Edit and delete button, if you are logged in


Click Edit, and edit


Save changes

The delete operation does not show here.

node. JS Blog Instance (v) Edit and delete features

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.