Node. js blog instance (5) Editing and deletion functions, node. js instance

Source: Internet
Author: User

Node. js blog instance (5) Editing and deletion functions, node. js instance

Original tutorial.

Now, you can edit or delete an article for a blog.

When a user is online, he or she is allowed to edit or delete only the published article pages. When editing, he or she can only edit the article content, not the title of the article.

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;}

Check whether the user name in the session exists. If the user name exists and is the same as the author name on the current article page, the edit and delete buttons are displayed. Otherwise, the edit and delete buttons are not displayed:

Article. ejs:

<%-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> <a class =" edit "href ="/remove/<% = post. name %>/<% = post. time. day %>/<% = post. title %> "> Delete </a> </span> <% }%> </p> <p class =" info "> author: <a href = "/u/<% = post. name %> "> <% = post. name %> </a> | Date: <% = post. time. minute %> </p> <%-post. post %> </p> <%-include footer %>
Post. js, add the following code:
// Return the original published content (in markdown format) Post. edit = function (name, day, title, callback) {// 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);} // query collection by user name, posting date, and article name. findOne ({"name": name, "time. day ": day," title ": title}, function (err, doc) {mongodb. close (); if (err) {return callback (err) ;}callback (null, doc ); // return an article (markdown format)}) ;}}) ;};}; // update an article and its related information Post. update = function (name, day, title, post, callback) {// 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);} // update the document 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 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);} // query and delete an article collection based on the user name, date, and title. 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/: day/: title', checkLogin); app. get ('/edit/: name/: day/: 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: 'edit', post: post, user: req. session. user, success: req. Flash ('success '). toString (), error: req. flash ('error '). toString ()}) ;}); app. post ('/edit/: name/: day/: title', checkLogin); app. post ('/edit/: name/: day/: 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. params. name + '/' + req. params. day + '/' + req. params. title; if (err) {Req. flash ('error', err); return res. redirect (url); // error! Back to Article Page} req. flash ('success ',' modified successfully! '); Res. redirect (url); // success! Back to Article Page}) ;}); app. get ('/remove/: name/: day/: title', checkLogin); app. get ('/remove/: name/: day/: 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', 'deleted successfully! '); Res. redirect ('/');});});
Create edit. ejs under blog/views/and 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 = "20" cols = "100"> <% = post. post %> </textarea> <br/> <input type = "submit" value = "Save changes"/> </form> <%-include footer %>
Finished! Try the effect, log on first, and then test the edit and delete operations:

Click to enter the article to see the edit and delete buttons, provided that you have logged on


Click Edit and edit


Save changes

The delete operation is not demonstrated here.


In nodejs, where are the descriptions of common functions found? Such as string processing, like asubstring?

Node. js uses google v8 as the javascript engine, so as long as it is a javascript object and function supported by chrome, it is available in node. js. The api on node's website is just something new to node. So you can see the javascript reference on mozilla's website. Developer.mozilla.org/en/docs/JavaScript/Reference

Which ide is generally used to edit nodejs?

I have been using sublime .. Install a node plug-in. However, I only use one plug-in to automatically complete console. log, and syntax highlighting... =. Actually, I am familiar with js. If I write node, I just need to open the node. org/apinet plug-in to the basics .. If you want any plug-in, go to npm and check readme if you don't know how to use it.
As for debugging, it is good to remember that a project will be restarted automatically when it is changed and saved, but the project is too busy to find it--It will always be manual ..
We recommend that you do not deliberately pursue an IDE such as automatic completion. In fact, it is not necessary to reduce the encoding speed. Because js is known for its flexibility, it is totally different from C # JAVA. If you write more C language, it is similar. I have been working on C # and JAVA for more than two years. After I switched to C, I was not used to having no idea about IDE at the beginning, but I had to stick to it for a few months.
However, vs has a node IDE. I used it for a moment and felt uncomfortable. Besides, I developed node on the linux platform, so it was useless at the end.

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.