nodejs實(shí)現(xiàn)登陸驗(yàn)證功能
本文實(shí)例為大家分享了nodejs實(shí)現(xiàn)登陸驗(yàn)證的具體代碼,供大家參考,具體內(nèi)容如下
登陸驗(yàn)證需要提交數(shù)據(jù),一種使用form表單提交數(shù)據(jù),另一種使用原生js提交數(shù)據(jù)
form表單提交
搭建后臺服務(wù)器
const express = require('express') const app = express() const bodyparser = require('body-parser') //掛載參數(shù)處理的中間件 //extended:false 表示使用系統(tǒng)模塊querystring來處理 將字符串轉(zhuǎn)化為對象 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請求后臺接口地址
原生js提交
const express = require('express') const app = express() const bodyparser = require('body-parser') //掛載參數(shù)處理的中間件 //extended:false 表示使用系統(tǒng)模塊querystring來處理 將字符串轉(zhuǎn)化為對象 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
node?NPM庫增強(qiáng)版globby?Promise使用學(xué)習(xí)
這篇文章主要為大家介紹了node?NPM庫增強(qiáng)版globby?Promise使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07node學(xué)習(xí)記錄之搭建web服務(wù)器教程
本篇文章主要介紹了詳解node學(xué)習(xí)記錄——搭建web服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02nodejs使用Sequelize框架操作數(shù)據(jù)庫的實(shí)現(xiàn)
這篇文章主要介紹了nodejs使用Sequelize框架操作數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Node.js實(shí)現(xiàn)數(shù)據(jù)推送
這篇文章主要為大家詳細(xì)介紹了Node.js實(shí)現(xiàn)數(shù)據(jù)推送的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-04-04nodejs基礎(chǔ)之buffer緩沖區(qū)用法分析
這篇文章主要介紹了nodejs基礎(chǔ)之buffer緩沖區(qū)用法,結(jié)合實(shí)例形式分析了buffer緩沖區(qū)的概念、功能、創(chuàng)建、讀寫等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12詳解如何優(yōu)雅在webpack項(xiàng)目實(shí)現(xiàn)mock服務(wù)器
這篇文章主要為大家介紹了詳解如何優(yōu)雅在webpack項(xiàng)目實(shí)現(xiàn)mock服務(wù)器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02