In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
这篇文章主要介绍"nodejs怎么实现登陆验证功能"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"nodejs怎么实现登陆验证功能"文章能帮助大家解决问题。
登陆验证需要提交数据,一种使用form表单提交数据,另一种使用原生js提交数据
form表单提交
搭建后台服务器
const express = require('express')const app = express()const bodyparser = require('body-parser')//挂载参数处理的中间件//extended:false 表示使用系统模块querystring来处理 将字符串转化为对象app.use(bodyparser.urlencoded({extended:false}))//挂载内置中间件处理静态文件app.use(express.static('public'))//使用form表单提交app.post('/login',(req,res)=>{ //因为是post,所以使用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文件
Document 用户名:
密码:
但该方法已经很很少使用了,现在主要使用ajax请求后台接口地址
原生js提交const express = require('express')const app = express()const bodyparser = require('body-parser')//挂载参数处理的中间件//extended:false 表示使用系统模块querystring来处理 将字符串转化为对象app.use(bodyparser.urlencoded({extended:false}))//挂载内置中间件处理静态文件app.use(express.static('public'))//使用form表单提交app.post('/login',(req,res)=>{ //因为是post,所以使用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....');}) Document $(()=>{ //按钮点击事件 $('#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('登陆失败') } } }) }) }) 用户名:
密码:
关于"nodejs怎么实现登陆验证功能"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.