express学习21-多人管理项目9登录功能具体实现

 

 blog.js

const express = require('express');
//创建网站服务器
const app = express();
//开放静态资源文件
const path = require('path');
require('./model/connect')
    //告诉express框架模板所在的位置
app.set('views', path.join(__dirname, 'views'));
//告诉express框架模板的后缀是什么
app.set('view engine', 'art');
//当渲染后缀为art的时候 搜索引擎是什么
app.engine('art', require('express-art-template'))
 
app.use(express.static(path.join(__dirname, 'public')));
//引入路由模块
 
const home = require('./homegeyao');
const admin = require('./admingeyao');
 
app.use('/home', home);
app.use('/admin', admin);
app.listen(3000);
 
console.log('服务器启动成功');

admingeyao.js

//管理页面
//展示页面
const express = require('express');
 
const admin = express.Router();
 
admin.get('/login', (req, res) => {
    res.render('admin/login')
});
admin.get('/user', (req, res) => {
    res.render('admin/user')
});
 
module.exports = admin;

 connect.js

// 引入mongoose第三方模块
const mongoose = require('mongoose');
// 连接数据库
mongoose.connect('mongodb://localhost/blog', {useNewUrlParser: true })
	.then(() => console.log('数据库连接成功'))
	.catch(() => console.log('数据库连接失败'))

user.js

// 创建用户集合
// 引入mongoose第三方模块
const mongoose = require('mongoose');
// 导入bcrypt
const bcrypt = require('bcrypt');
// 引入joi模块
const Joi = require('joi');
// 创建用户集合规则
const userSchema = new mongoose.Schema({
	username: {
		type: String,
		required: true,
		minlength: 2,
		maxlength: 20
	},
	email: {
		type: String,
		// 保证邮箱地址在插入数据库时不重复
		unique: true,
		required: true
	},
	password: {
		type: String,
		required: true
	},
	// admin 超级管理员
	// normal 普通用户
	role: {
		type: String,
		required: true
	},
	// 0 启用状态
	// 1 禁用状态
	state: {
		type: Number,
		default: 0
	}
});
 
// 创建集合
const User = mongoose.model('User', userSchema);
 
async function createUser () {
	const salt = await bcrypt.genSalt(10);
	const pass = await bcrypt.hash('123456', salt);
	const user = await User.create({
		username: 'iteheima',
		email: 'itheima@itcast.cn',
		password: pass,
		role: 'admin',
		state: 0
	});
}
 
// createUser();
 
// 验证用户信息
const validateUser = user => {
	// 定义对象的验证规则
	const schema = {
		username: Joi.string().min(2).max(12).required().error(new Error('用户名不符合验证规则')),
		email: Joi.string().email().required().error(new Error('邮箱格式不符合要求')),
		password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required().error(new Error('密码格式不符合要求')),
		role: Joi.string().valid('normal', 'admin').required().error(new Error('角色值非法')),
		state: Joi.number().valid(0, 1).required().error(new Error('状态值非法'))
	};
 
	// 实施验证
	return Joi.validate(user, schema);
}
 
// 将用户集合做为模块成员进行导出
module.exports = {
	User,
	validateUser
}

login.art

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <link rel="stylesheet" href="/admin/lib/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/admin/css/base.css">
</head>
 
<body>
    <div class="login-body">
        <div class="login-container">
            <h4 class="title">黑马程序员 - 博客管理员登录</h4>
            <div class="login">
                <form action="/admin/login" method="post" id="loginForm">
                    <div class="form-group">
                        <label>邮件</label>
                        <input name="email" type="email" class="form-control" placeholder="请输入邮件地址">
                    </div>
                    <div class="form-group">
                        <label>密码</label>
                        <input name="password" type="password" class="form-control" placeholder="请输入密码">
                    </div>
                    <button type="submit" class="btn btn-primary">登录</button>
                </form>
            </div>
            <div class="tips"></div>
        </div>
    </div>
    <script src="/admin/lib/jquery/dist/jquery.min.js"></script>
    <script src="/admin/lib/bootstrap/js/bootstrap.min.js"></script>
    <script src="/admin/js/common.js"></script>
    <script type="text/javascript">
        // 为表单添加提交事件
        $('#loginForm').on('submit', function () {
            // 获取到表单中用户输入的内容
            var result = serializeToJson($(this))
            // 如果用户没有输入邮件地址的话
            if (result.email.trim().length == 0) {
                alert('请输入邮件地址');
                // 阻止程序向下执行
                return false;
            }
            // 如果用户没有输入密码
            if (result.password.trim().length == 0) {
                alert('请输入密码')
                // 阻止程序向下执行
                return false;
            }
        });
    </script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值