您好,登錄后才能下訂單哦!
這篇文章主要介紹“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ù)
搭建后臺(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)接口地址
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)。
免責(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)容。