宝哥软件园

在node.js平台下用cookie表达Ejs Mysql

编辑:宝哥软件园 来源:互联网 时间:2021-09-05

这个内容需要有node.js express mysql入门的基础。如果基础薄弱,请参考博主node.js的其他博文:

1.下载Mysql数据库,安装并配置。为登录创建用户表:

2.2.node.js平台下Express的会话和cookie模块包的配置://www . JB 51 . net/article/112190 . htm

3.3.node.js平台下的mysql数据库配置与连接://www . JB 51 . net/article/110079 . htm

完成前两个步骤后,下载并配置Ejs模块包:

*下载ejs模块包:npm安装ejs -保存-开发

*配置ejs:

/*设置模板资源路径*/app . set(' view ',_ _ dirname '/view ');//视图模板都在这个文件夹里。/*自定义文件后缀,设置模板引擎*/app.engine ('html ',ejs。_ _快递)。app.set('视图引擎',' html ');//设置模板引擎,这意味着视图的后缀是ejs4。登录页面(login.html)

div class=' register BG ' section class=' register box BD ' div class=' reg title ' log in/div form method=' post ' action='/Enter log in . do ' input type=' text ' class=' phone ' placeholder='请输入您的手机号码' Id=' phone ' name=' phone ' input type=' password ' placeholder='请输入您的密码' class=' pwd ' name=' pwd ' Id=' pwd '/div class=' otherbd ' label class=' obeybd ' input type='/a/div button class=' doreg ' id=' dolo gin '登录名/按钮/表单/节/div5。点击登录界面的登录按钮后,服务器入口文件(app.js)截取路由/login.do。

const per=require('。/routes/PErDATa . js ');//服务器入口文件引入perdata.jsapp.post ('/login.do ',per . dolo gin);6.截取路由后,将其分发到路由处理文件(perData.js)中,该文件公开了/login.do的接口并提供了处理功能。

////登录验证导出。dologin=function (req,RES) {console.log (req。body . phone);console . log(req . body . pwd);db.connect('从t_user中选择*其中u_tel=?和u_pwd=?',[req.body.phone,req.body.pwd],函数(err,data){ console . log(data . length);如果(data.length0){//这里需要判断用户是否点击记住了login.html的密码,方便理解。默认用户已单击“记住密码”//以确定用户是否单击了checkBox: //1。本地js:文档。getelementbyid ('rempwd ')。选中//2。jquery: $ ('# rempwd ')。is(' : checked ')RES . cookie(' user ',{'user':req.body.phone,' pwd':req.body.pwd},{ maxage :1000 * 60 * 60 });//成功登录后将用户和密码写入cookie,maxAge为Cookie过期时间req . session . user=req . body . phone;//服务器端会话保存登录的会话状态res.render ('percenter ',{ u _ tel : req . session . user });//ejs模板引擎渲染用户中心页面(perCenter.js)并返回u_tel数据到前台} });*perCenter.js视图页面如下,%=u_tel%是ejs语法的定义变量,后台返回的u_tel直接嵌入到视图页面中。

div class=' navBox container-fluid ' div class=' row ' Span class=' col-LG-2 col-MD-2 icon-lianxi contact alignLeft ' 010-65596969/Span Span class=' col-LG-2col-MD-2con-denglucontact col-LG-push-8col-MD-push-2 alignLeft ' %=u _ tel %/Span/div/div 7。整个登录过程完成,用户点击“记住密码”后,用户信息被写入cookie中,现在需要改进的是,用户关闭浏览器窗口,cookie未过期后,第二次登录网站时会直接显示登录状态,需要截取服务器入口文件(app.js)中的服务器根路由,根据cookie进行判断。

如果app.get ('/')中有用户信息,函数(req,res) {if (req。cookies.user) {//cookie,它会直接返回到登录页面res.render ('percenter ',{u _ tel : req。cookies . user . user })} else { RES .//否则网站index.html页面将正常显示} });*附上app.js配置文件的所有内容:

const express=require(' express ');const app=express();const session=require(' express-session ');const cookie=require(' cookie-parser ');const ejs=require(' ejs ');const per=require(' ./routes/PErDATa。js’);app。configure(function(){ app。使用(cookie());app。use(session({ name : ' final ',secret:'1234567 ',cookie: { maxAge:10000 },//毫秒为单位resave:true,roll : true });app。set(' view ',_ _ dirname '/view ');app.engine('html ',ejs ._ _快递);app.set('视图引擎,' html ');app。使用(快递。记录器(' dev ');app。使用(快递。body parser());app。使用(快递。方法重写());app。使用(app。路由器);app。使用(快递。static(_ _ dirname '/public ');//app。使用(快递。fav图标(_ _ dirname '/public/images/fav图标。ico’);app。使用(快递。错误处理程序());});app.set('port ',8889);app.listen(app.get('port '),function(){ console.log('启动成功app。get(' port ')});/*======路由分发======*/app.get('/'),函数(req,RES){ if(req。饼干。用户){ RES . render(' perCenter ',{ u _ tel : req。饼干。用户。user })} else { RES . redirect(' index。html ');}});app.post('/Login.do ',per。dolo gin);以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

更多资讯
游戏推荐
更多+