-1
如何使用node.js,express.js和mongo db创建restful api。我需要如何编写登录架构和注册页面,以及如何使用nodejs express js将数据与mongodb进行比较。如何使用node.js创建简单的restful api登录和注册mongodb express.js
我是初学者到nodejs请帮助我一些例子。
如何使用node.js,express.js和mongo db创建restful api。我需要如何编写登录架构和注册页面,以及如何使用nodejs express js将数据与mongodb进行比较。如何使用node.js创建简单的restful api登录和注册mongodb express.js
我是初学者到nodejs请帮助我一些例子。
//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();
};
感谢@Shekar特亚吉 –
嗯,这可能会帮助你[使用猫鼬/ MongoDB的简单登录 - Node.js的教程](https://www.youtube.com/watch?v=pzGQMwGmCnc&list=PLVBXNyNyLNq3MGbopdcvWc25xijtWaA6X&index=17) – Nane