前端部分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登入注册</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 550px;
background-image: url('../../img//002R448Kgy1hkeh2jokj4j62dc4qnnpg02.jpg');
background-size: 100%;
background-repeat: no-repeat;
background-position: center;
position: relative;
}
.box{
width: 400px;
height: 500px;
border: 2px solid black;
position: absolute;
left: 50%;
top: 50%;
margin-left: -200px;
margin-top: -250px;
}
.box form{
width: 100%;
height: 100%;
}
.box form h1{
text-align: center;
color: aliceblue;
}
.box form span{
display: inline-block;
width: 70px;
height: 20px;
/* border: 1px solid red; */
margin-left: 40px;
margin-top: 40px;
text-align: right;
}
.box form input{
height: 20px;
outline: none;
}
.box .tress{
width: 50px;
margin-left: 130px;
margin-top: 40px;
text-align: center;
font-weight: 600px;
cursor: pointer;
}
.box .four a{
text-decoration: none;
border: 0.1px solid black;
color:red;
font-size: 14px;
font-weight: 600px;
text-align: center;
cursor: pointer;
background-color: aliceblue;
}
</style>
</head>
<body>
<div class="box">
<form action="http://localhost:8080/login" method="get">
<h1>登入</h1>
<span>用户名:</span><input type="text" name ='account'><br>
<span>密码:</span><input type="password" name="password"><br>
<!-- <span>确认密码:</span><input type="password" ><br> -->
<input type="submit" value="登入" class="tress">
<span class="four"><a href="../public/resing.html">点击注册</a></span>
</form>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>注册</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
height: 550px;
background-image: url('../../img//001YqBPrly1hk56d2pcdej644o5pukju02.jpg');
background-size: 100%;
background-repeat: no-repeat;
background-position: center;
position: relative;
}
.box{
width: 400px;
height: 500px;
border: 2px solid black;
position: absolute;
left: 50%;
top: 50%;
margin-left: -200px;
margin-top: -250px;
}
.box form{
width: 100%;
height: 100%;
}
.box form h1{
text-align: center;
color: aliceblue;
}
.box form span{
display: inline-block;
width: 100px;
height: 20px;
/* border: 1px solid red; */
margin-left: 40px;
margin-top: 40px;
text-align: right;
}
.box form input{
height: 20px;
outline: none;
}
.box .tress{
width: 50px;
margin-left: 130px;
margin-top: 40px;
text-align: center;
font-weight: 600px;
cursor: pointer;
}
.box .four{
}
.box .four a{
text-decoration: none;
border: 0.1px solid black;
color:red;
font-size: 14px;
font-weight: 600px;
text-align: center;
cursor: pointer;
background-color: aliceblue;
}
</style>
</head>
<body>
<div class="box">
<form action="http://localhost:8080/process" method="get">
<h1>注册</h1>
<span>用户名:</span><input type="text" name ='account'><br>
<span>密码:</span><input type="password" name="password"><br>
<span>确认密码:</span><input type="password" name="repassword"><br>
<input type="submit" value="注册" class="tress">
<span class="four"><a href="../public//index.html">返回登入</a></span>
</form>
</div>
</body>
</html>
后端部分
服务器启动部分
const express = require('express')
const router = require('./router')
const cors = require('cors')
const server = express()
erver.use(cors())
server.use('/',router)
server.listen(8080,()=>{
console.log('server start');
})
动态路由部分
const express = require('express')
const {index,register} = require('./index')
const router = express.Router()
router.get('/login',index)
router.get('/process',register)
module.exports = router
数据库连接部分
const mysql = require('mysql')
const db = mysql.createConnection({
host:'localhost',
port:3306,
user:'root',
password:'197386',
database: 'node'
})
module.exports = db
动态路由请求部分
const mysql = require('./mysql')
const index =function (req,res) {
var response = {
"account":req.query.account,
"password":req.query.password,
};
var selectSQL = "select name,password from user where name = '"+req.query.account+"' and password = '"+req.query.password+"'";
mysql.query(selectSQL,function (err, result)
if(err){
console.log('[login ERROR] - ',err.message);
return;
}
//如果查
询结果为空,则登录失败,否则登录成功
if(result=='')
{
console.log("帐号密码错误");
res.end("fail");
}
else
{
console.log("登录成功");
res.end("success");
}
});
console.log(response);
}
const register = function(req,res,next){
var result = {
"account":req.queryaccount,
"password":req.querypassword,
"repassword":req.query.repassword
}
if(req.query.password === req.query.repassword){
var selectSQL = 'INSERT INTO user(name,password,repassword) VALUE (?,?,?)';
var addSqlParams = [req.query.account,req.query.password,req.query.repassword]
mysql.query(selectSQL,addSqlParams,function (err, result) {
// 打印错误信息
if(err){
console.log('[INSERT ERROR] - ',err.message);
res.end("fail");
//如果失败就直接return不会执行下面的代码
return;
}
res.end("success");
console.log("注册成功");
});
console.log(result);
}else{
console.log("帐号密码错误");
res.end('password not repassword')
}
}
module.exports = {
index,
register
}