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