在node.js和mongodb中创建注册和登录表单 [英] creating registration and login form in node.js and mongodb
问题描述
我是node.js的新手,想要为user.also创建一个注册和登录页面,而且还有适当的用户授权。我想将用户信息存储在mongodb数据库中。我如何实现这个。有人向我提供这样做的代码,以便我可以开始使用node.js和mongodb.Please帮助
I am new to node.js and want to create a registration and login page for user.also there has to proper authorisation for the user.I want to store the user information inside mongodb database.How can i achieve this.can someone provide me the code to do so, so that i can get started with node.js and mongodb.Please help
推荐答案
你可以在亚历克斯·杨的Nodepad应用程序中找到您要尝试的内容的完整示例。你应该看看的2个重要文件是这些2:
https://github.com/alexyoung/nodepad/blob/master/models.js
https://github.com/alexyoung/nodepad/blob/master/app.js
You can find a complete sample of what you're trying to do in the Nodepad application by Alex Young. The 2 important file you should take a look at are these 2:
https://github.com/alexyoung/nodepad/blob/master/models.js
https://github.com/alexyoung/nodepad/blob/master/app.js
模型看起来像这样:
User = new Schema({
'email': { type: String, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } },
'hashed_password': String,
'salt': String
});
User.virtual('id')
.get(function() {
return this._id.toHexString();
});
User.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
})
.get(function() { return this._password; });
User.method('authenticate', function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
});
User.method('makeSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
User.method('encryptPassword', function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
});
User.pre('save', function(next) {
if (!validatePresenceOf(this.password)) {
next(new Error('Invalid password'));
} else {
next();
}
});
我想他也在dailyjs网站上解释代码。
这篇关于在node.js和mongodb中创建注册和登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!