Kill lui lei Dog---node. js---20 project build in Node+express+mongo Blog Project 5mongodb implement add data in a project

Source: Internet
Author: User
Tags mongoclient

We introduced MongoDB in the last step, then the next step is to start writing add data, but there is a prerequisite for the data processing of the form first:

The most basic of this has now been successful, because the most basic step is to first the form at the submission method and submit address to deal with, here and PHP is basically the same ...

Then we receive the content in the backend processing:

has been successfully received at the back end, then the next step is to directly let him into the storage ...

/** * Created by Leigood on 2016/8/31.*/varExpress = require (' Express ');varRouter =Express. Router ();//Introducing the MongoDB modulevarMongoclient = require (' MongoDB '). mongoclient;varDb_str = "Mongodb://localhost:27017/blog";//Here's a library built under MongoDB.varObjectId = require (' MongoDB '). ObjectId;//This is mainly used to deal with MONGODB under the ID/*GET users listing.*/Router.get (‘/‘,function(req, res, next) {Res.render (' Admin/category ');}); Router.get ('/add ',function(Req,res,next) {Res.render (' Admin/category_add ');}); Router.post ('/add ',function(req,res) {vartitle =Req.body.title; varSort =Req.body.sort; //console.log (title+ '-------' +sort);Mongoclient.connect (DB_STR,function(err,db) {if(err) {Throwerr; return; }        //The db here is the blog database        varc = db.collection (' category '); C.insert ({title:title,sort:sort},function(err,result) {if(Err) {err.send (err); }Else{res.send (' Add category Success <a href= '/admin/category ' > View list </a> ');    }        }); });}); Router.get ('/edit ',function(Req,res,next) {Res.render (' Admin/category_edit ');}); Module.exports= Router;

The effect of the display is as follows:

Kill lui lei Dog---node. js---20 project build in Node+express+mongo Blog Project 5mongodb implement add data in a project

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.