In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to achieve registration and login function in NodeJs+MySQL". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope that this article "how to achieve registration and login function in NodeJs+MySQL" can help you solve the problem.
How to write mysql in nodejs:
/ / connection pool let db=mysql.createPool ({'configuration'}) db.query (`sql statement `, (err,data) = > {})
And the password stored in the database should be ciphertext.
Function md5 (str) {let obj=crypto.createHash ('md5'); obj.update (str); return obj.digest (' hex')} function md5_2 (str) {return md5 (md5 (str))}
Server operation, a simple check is added to the user name and password, which is not perfect and needs to be improved.
Const http=require ('http'); const fs=require (' fs'); const mysql=require ('mysql'); const url=require (' url'); const zlib=require ('zlib'); const crypto=require (' crypto'); const _ key='bsjhjqbj1;dqwxsxx+'; let server=http.createServer ((req,res) = > {let {pathname,query} = url.parse (req.url,true); let {user,password} = query Switch (pathname) {case'/ reg': if (! user) {res.write ('{"err": 1, "msg": "user name cannot be empty"}'); res.end ();} else if (! password) {res.write ('{"err": 1, "msg": "password cannot be empty"}'); res.end () } else if (! /\ w {4Magne16} $/ .test (user)) {res.write ('{"err": 1, "msg": "username should be uppercase and lowercase alphanumeric or underscore"}'); res.end ();} else if (/ ['| "] / .test (password)) {res.write ('{" err ": 1," msg ":" password is illegal "}') Res.end ();} else {db.query (`SELECT username FROM users_table WHERE username='$ {user}', (err,data) = > {if (err) {res.write ('{"err": 1, "msg": "database error"}')) Console.log (err) res.end ()} else {if (data.length > 0) {res.write ('{"err": 1, "msg": "user name already exists"}'); res.end () } else {res.write ('{"err": 0, "msg": "registered successfully"}'); db.query (`INSERT INTO users_table (ID,username,password) VALUES (0meme ${user}','${md5_2 (password)}') `); res.end ();} break Case'/ login': if (! user) {res.write ('{"err": 1, "msg": "user name cannot be empty"}'); res.end ();} else if (! password) {res.write ('{"err": 1, "msg": "password cannot be empty"}'); res.end () } else if (! /\ w {4Magne16} $/ .test (user)) {res.write ('{"err": 1, "msg": "user name should be uppercase and lowercase alphanumeric or underscore"}'); res.end ()} else if (/ ["|] / .test (password)) {res.write ('{" err ": 1," msg ":" password is illegal "}') Res.end ();} else {db.query (`SELECT username,password FROM users_table WHERE username='$ {user}', (err,data) = > {if (err) {res.write ('{"err": 1, "msg": "database error"}')) Res.end ()} else if (data.length > 0) {if (md5_2 (password)! = data [0] .password) {res.write ('{"err": 1, "msg": "incorrect username or password"}'); res.end () } else {res.write ('{"err": 0, "msg": "login successful"}'); res.end ();}} else {res.write ('{"err": 1, "msg": "user does not exist")} break)} Default: let rs=fs.createReadStream (`www$ {pathname} `); let gz=zlib.createGzip (); res.setHeader ('content-encoding','gzip'); rs.pipe (gz) .pipe (res); rs.on (' error',err= > {res.writeHeader (404); res.write ('Not Found'); res.end ();}); server.listen (8888) This is the end of the content about "how to achieve the registration and login function of NodeJs+MySQL". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.