今天又是感觉非常充实的一天,因为我花了好几个小时研究我的bug
我来说一下我的bug吧,我今天用express写了几个接口,使用session认证用户身份,本来
十几分钟都可以搞定的事情我花了一两个小时,主要是两个地方的问题。讴歌是单词的拼写,在app.js代码中send写成了end,可能是当时写的太快了,然后接写错了,找了大半天,第二个就是使用postman测试post接口的时候一定要注意以下两种轻快是不一样的
情况一
情况二:
这里面的username是两个值,一定要分清楚,我当时就不知道,花了我好长时间才发现
下面是我的代码部分:
结构目录:
index.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>Document</title>
<script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h1>首页</h1>
<button id="ltnLogout">退出登录</button>
<script>
$.ajax({
type: 'GET',
url: 'http://127.0.0.1/api/username',
success: function(res) {
console.log(res)
console.log(res.status)
if (res.status == 1) {
alert('亲您先登录成功在进行访问')
window.location.href = 'http://127.0.0.1/login.html';
} else {
alert('欢迎光临')
}
}
})
$('button').on('click', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://127.0.0.1/api/logout',
success: function(res) {
window.location.href = 'http://127.0.0.1/login.html';
alert('退出成功')
}
})
})
</script>
</body>
</html>
login.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>Document</title>
<script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
<style>
form {
width: 250px;
height: 100px;
margin: 0 auto;
background-color: pink;
border-radius: 20px;
text-align: center;
line-height: 34px;
}
</style>
</head>
<body>
<form id="form">
<div>账号:<input type="text" name="username" autocomplete="off" class="a"></div>
<div>密码:<input type="password" name="password" autocomplete="off" class="b"></div>
<button type="button">登录</button>
</form>
<script>
$(function() {
$('button').on('click', function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://127.0.0.1/api/login',
data: {
username: $('.a').val(),
password: $('.b').val()
},
success: function(res) {
console.log(res)
console.log(res.status)
if (res.status == 1) {
alert('您填写的用户名或密码不对')
} else {
window.location.href = 'http://127.0.0.1/index.html';
}
}
})
})
})
</script>
</body>
</html>
app.js:
const express = require('express')
const app = express()
const session = require('express-session')
app.use(
session({
secret: 'lsh',
resave: false,
saveUninitialized: true,
})
)
const cors = require('cors')
app.use(cors())
app.use(express.static('./page'))
app.use(express.urlencoded({ extended: false }))
app.post('/api/login', (req, res) => {
if (req.body.username !== 'lsh' || req.body.password !== '000000')
return res.send({ status: 1, msg: '登陆失败' })
req.session.islogin = true
req.session.user = req.body
res.send({ status: 0, msg: '登陆成功' })
})
app.get('/api/username', (req, res) => {
if (!req.session.islogin) {
return res.send({ status: 1, msg: 'fail' })
}
res.send({
status: 0,
msg: 'succes',
username: req.session.user.username
})
})
app.post('/api/logout', (req, res) => {
req.session.destroy()
res.send({ status: 0, msg: '退出登陆成功' })
})
app.listen(80, () => {
console.log('客户端启动成功: 127.0.0.1:80')
})
最终的结果如下:
1.正确的用户名与密码
2.不正确的用户名与密码
3.直接访问index.html