About node in mongodb database CRUD

var bodyParser the require = ( ' body-Parser ' )
 var Mongoose = the require ( ' Mongoose ' ); 

// ********************************************************* MongoDB database ********* **********
 // connect to the database 
mongoose.Promise = Global .Promise; 
mongoose.connect ( ' MongoDB: // localhost / TODO ' , {useMongoClient: to true }) 
.then (function () {Console .log ( ' MongoDB database connection success ' )}) 
. the catch (function (ERR) {the console.log (ERR)}) 

// create the database structure, the table name is todolists 
var= Mongoose.model ToDoList ( ' ToDoList ' , { 
    ID: Number The, 
    DONE: Boolean, 
    title: String 
}) 

// test code: todolists table stored in the database data
 // ToDoList ({ID: 100, DONE: to false, title :. "eat"}) save (function (ERR, Data) {
 //      IF (ERR) the throw ERR;
 //      the console.log ( 'successfully saved')
 // }) 

// ******** ******** MongoDB database ******************* 

// exposed outward function, receiving the object app 
module.exports = function (app) { 
    app. use (bodyParser.urlencoded ({Extended: to false })) 

    // 1. request (GET) for acquiring data 
    App. GET( " / " , Function (REQ, RES) {
         // query for all data in the database 
        TodoList.find ({}, function (ERR, Data) {
             // All data returned to the front end is rendered Home 
            res.render ( " index " , { " Todos " : Data}); 
        }) 
    }) 


    // ********** following start interception request initiated by various front-end *********** 

    // 2. POST request to submit data interception distal 
    app.post ( " / the Add " , function (REQ, RES) {
         var obj = {ID: Date.now (), DONE: to false , title: req.body.title} 

        / / insert the data into the database, and generates a not duplicate primary key id
        ToDoList (obj) .save (function (ERR, Data) {
             IF (ERR) the throw ERR; 
            res.send ( ' OK ' ) 
        }) 
    }) 


    // 3. knockdown distal POST data request submitted 
    app.delete ( " / del /: id " , function (REQ, RES) {
         var id = Number The (. REQ the params .id); 

        // query the database, delete data in accordance with id 
        TodoList.find ({ " id " : id}) remove (function. (ERR, Data) {
             IF (ERR) the throw ERR; 
            res.send ( ' OK ') // returns the response result to the distal 
        }) 
    }) 


    // the PUT request to modify the front end 4. intercept, modify the state agency matters 
    app.put ( " / Update /: ID " , function (REQ, RES) {
         var ID = Number The (REQ. the params .id); 

        // modify dos state (yes / no) 
        TodoList.findOne ({ " ID " : ID}, function (ERR, Data) { 
            data.done ! = data.done; // assignment modified 
            data.save (); // save 
            res.send ( ' OK ' ); // returns the response result to the distal 
        }) 
    })


    // The front end of the interception PATCH modification request, the content modification dos 
    app.patch ( " / Update /: ID " , function (REQ, RES) {
         var . = Number The ID (REQ the params .id); 

        // Review title content-do, methods. 1
         // TodoList.findOne ({ "ID": ID}, function (ERR, Data) {
         //      data.title = req.body.title; // assignment modifications
         //      data.save (); // save
         //      res.send ( 'OK'); // returns the response result to the distal
         @ }) 

        TodoList.update ({ " ID " : ID}, {$ SET : {" Title " : req.body.title}}, function (ERR, Data) { 
            res.send ( ' OK ' ); // returns the response result to the distal 
        }) 
    }) 
}

 

Guess you like

Origin www.cnblogs.com/xuyx/p/11866941.html