2016-12-07 75 views
-1

如何使用node.js,express.js和mongo db創建restful api。我需要如何編寫登錄架構和註冊頁面,以及如何使用nodejs express js將數據與mongodb進行比較。如何使用node.js創建簡單的restful api登錄和註冊mongodb express.js

我是初學者到nodejs請幫助我一些例子。

+1

嗯,這可能會幫助你[使用貓鼬/ MongoDB的簡單登錄 - Node.js的教程](https://www.youtube.com/watch?v=pzGQMwGmCnc&list=PLVBXNyNyLNq3MGbopdcvWc25xijtWaA6X&index=17) – Nane

回答

2
//the simple example of login// 

     router.post('/users/login', function (req, res) { 
      var users = req.app; 
      var email = req.body.email; 
      var password = req.body.password; 
      if (email.length > 0 && password.length > 0) { 
       users.findOne({email: email, password: password}, function (err, user) { 
        if (err) { 
         res.json({status: 0, message: err}); 
        } 
        if (!user) { 
         res.json({status: 0, msg: "not found"}); 
        } 
        res.json({status: 1, id: user._id, message: " success"}); 
       }) 
      } else { 
       res.json({status: 0, msg: "Invalid Fields"}); 
      } 
     }); 

    //and if you have to create schema 

var db_schema = new Schema({ 
       email: {type: String, required: true, unique: true}, 
       password: {type: String, required: true, unique: true}, 
      }); 
// define this in your db.js 
     var login_db = mongoose.model('your_db_name', db_schema); 
     return function (req, res, next) { 
       req.app = login_db; 
       next(); 
      }; 
+0

感謝@Shekar特亞吉 –

相關問題