[英]How to create simple restful api for login and signup using node.js mongodb express.js
How to create restful api using node.js, express.js and mongo db. 如何使用node.js,express.js和mongo db创建Restful API。 I need how to write the schema for login and sign up pages and how to compare the data with mongodb using nodejs express js.
我需要如何为登录和注册页面编写模式,以及如何使用nodejs express js将数据与mongodb进行比较。
I am beginner to nodejs please help me with some examples. 我是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();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.