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表單提交
搭建后臺(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>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Node.js創(chuàng)建一個(gè)Express服務(wù)的方法詳解
這篇文章主要介紹了Node.js創(chuàng)建一個(gè)Express服務(wù)的方法,結(jié)合實(shí)例形式分析了node.js創(chuàng)建Express服務(wù)的具體步驟、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
node.js中的buffer.toString方法使用說(shuō)明
這篇文章主要介紹了node.js中的buffer.toString方法使用說(shuō)明,本文介紹了buffer.toString的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12
nodejs實(shí)現(xiàn)簡(jiǎn)單的gulp打包
因?yàn)橹耙恢庇腥私o我推薦gulp,說(shuō)他這里好哪里好的。實(shí)際上對(duì)我來(lái)說(shuō)夠用就行。grunt熟悉以后實(shí)際上他的配置也不難,說(shuō)到效率的話確實(shí)是個(gè)問(wèn)題,尤其項(xiàng)目大了以后,目前位置遇到的項(xiàng)目都還可以忍受。不過(guò)不管怎么說(shuō),需要親自用過(guò)gulp之后才能品評(píng)他和grunt之間的優(yōu)劣。2017-12-12
node 利用進(jìn)程通信實(shí)現(xiàn)Cluster共享內(nèi)存
本篇文章主要介紹了node 利用進(jìn)程通信實(shí)現(xiàn)Cluster共享內(nèi)存,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Node.js 利用cheerio制作簡(jiǎn)單的網(wǎng)頁(yè)爬蟲示例
本篇文章主要介紹了Node.js 利用cheerio制作簡(jiǎn)單的網(wǎng)頁(yè)爬蟲示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
node.js中的fs.appendFileSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.appendFileSync方法使用說(shuō)明,本文介紹了fs.appendFileSync方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12

