溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nodejs怎么實(shí)現(xiàn)登陸驗(yàn)證功能

發(fā)布時(shí)間:2022-04-27 09:10:09 來(lái)源:億速云 閱讀:181 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“nodejs怎么實(shí)現(xiàn)登陸驗(yàn)證功能”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“nodejs怎么實(shí)現(xiàn)登陸驗(yàn)證功能”文章能幫助大家解決問(wèn)題。

登陸驗(yàn)證需要提交數(shù)據(jù),一種使用form表單提交數(shù)據(jù),另一種使用原生js提交數(shù)據(jù)

form表單提交

搭建后臺(tái)服務(wù)器

const express = require('express')
const app = express()
const bodyparser = require('body-parser')
//掛載參數(shù)處理的中間件
//extended:false 表示使用系統(tǒng)模塊querystring來(lái)處理 將字符串轉(zhuǎn)化為對(duì)象
app.use(bodyparser.urlencoded({extended:false}))
//掛載內(nèi)置中間件處理靜態(tài)文件
app.use(express.static('public'))

//使用form表單提交
app.post('/login',(req,res)=>{
    //因?yàn)槭莗ost,所以使用body
    let data = req.body;
    //判斷用戶名和密碼
    if(data.username=='admin'&&data.password=='123'){
        res.send('登陸成功')
    }else{
        res.send('登陸失敗')
    }
})
app.listen(3000,()=>{
    console.log('running....');
})

public目錄下的login.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="http://localhost:3000/login" method="post">
        用戶名:
        <input type="text" name="username" id="use"><br>
        密碼:
        <input type="password" name="password" id="pwd"><br>
        <!-- <input type="submit" value="登錄"> -->
        <input type="button" value="登錄" id="btn">
    </form>
</body>
</html>

但該方法已經(jīng)很很少使用了,現(xiàn)在主要使用ajax請(qǐng)求后臺(tái)接口地址

原生js提交

const express = require('express')
const app = express()
const bodyparser = require('body-parser')
//掛載參數(shù)處理的中間件
//extended:false 表示使用系統(tǒng)模塊querystring來(lái)處理 將字符串轉(zhuǎn)化為對(duì)象
app.use(bodyparser.urlencoded({extended:false}))
//掛載內(nèi)置中間件處理靜態(tài)文件
app.use(express.static('public'))

//使用form表單提交
app.post('/login',(req,res)=>{
    //因?yàn)槭莗ost,所以使用body
    let data = req.body;
    //判斷用戶名和密碼
    if(data.username=='admin'&&data.password=='123'){
        res.send('登陸成功')
    }else{
        res.send('登陸失敗')
    }
})

app.get('/login',(req,res)=>{
    let data = req.query;
    
    if(data.username=='admin'&&data.password=='123'){
        res.send({flag:1})
    }else{
        res.send({flag:2})
    }
})
app.listen(3000,()=>{
    console.log('running....');
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!--引入jQuery-->
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script>
        $(()=>{
            //按鈕點(diǎn)擊事件
            $('#btn').click(()=>{
                //獲取輸入框中的值
                let use = $('#use').val()
                let pwd = $('#pwd').val()
                $.ajax({
                    //type后為字符串
                    type:'get',
                    url:'http://localhost:3000/login',
                    data:{
                        username:use,
                        password:pwd,
                        
                    },
                    success:(data)=>{
                            if(data.flag==1){
                                alert('登陸成功')
                            }else{
                                alert('登陸失敗')
                            }
                        }
                })
            })
        })
    </script>
</head>
<body>
    <form action="http://localhost:3000/login" method="post">
        用戶名:
        <input type="text" name="username" id="use"><br>
        密碼:
        <input type="password" name="password" id="pwd"><br>
        <!-- <input type="submit" value="登錄"> -->
        <input type="button" value="登錄" id="btn">
    </form>
</body>
</html>

關(guān)于“nodejs怎么實(shí)現(xiàn)登陸驗(yàn)證功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI