dm/app-dm/server/router.js

54 lines
1.3 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const validator = require('validator');
const isEmpty = require('lodash/isEmpty')
// const sqlFn = require('./config') // 未使用
// { 判断注册表单是否为空 }
/**
* 如果发生错误则返回错误信息如果没发生错误则返回true
*/
const validatorInput = (data) =>{
/**
* validator.isEmpty方法验证是否为空
*/
let errors={}
if (validator.isEmpty(data.name)){
errors.name = '用户名不能为空'
}
if (validator.isEmpty(data.ID)){
errors.ID = '学号不能为空'
}
if (validator.isEmpty(data.password)){
errors.password = '密码不能为空'
}
if (validator.isEmpty(data.Class)){
errors.Class = '班级不能为空'
}
if (validator.isEmpty(data.gender)){
errors.gender = '性别不能为空'
}
return{
//如果value为空那么返回true否则返回false
isValid:!isEmpty(errors),
errors
}
}
router.post('/register',(req,res) =>{
const { isValid,errors } = validatorInput(req.body)
if(isValid){
//失败
res.send(errors)
}else{
//成功
res.send({
msg:"seccess"
})
}
})
module.exports = router;