2016-12-07 4 views
-1

node.js、express.js、mongo dbを使用して安らかなAPIを作成する方法。ログイン用のスキーマを作成してページを登録する方法と、nodejs express jを使用してmongodbとデータを比較する方法が必要です。ログインとサインアップのための簡単な安らかなAPIの作成方法node.jsを使用して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ティアギ(Tyagi) –

関連する問題