bug记录-postman测试接口

今天又是感觉非常充实的一天,因为我花了好几个小时研究我的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
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白嫖leader

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值