NodeJs+MySQL實(shí)現(xiàn)注冊(cè)登錄功能
本文實(shí)例為大家分享了NodeJs+MySQL實(shí)現(xiàn)注冊(cè)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
之前寫過(guò)一個(gè)沒(méi)有連接數(shù)據(jù)庫(kù)的注冊(cè)與登陸的實(shí)現(xiàn),這次加上了數(shù)據(jù)庫(kù)
剛剛接觸后端,很多不完善的地方,有錯(cuò)誤請(qǐng)指正
nodejs中mysql的寫法:
//連接池
let db=mysql.createPool({'配置'}) ??
db.query(`sql語(yǔ)句`,(err,data)=>{})并且在數(shù)據(jù)庫(kù)中存儲(chǔ)的密碼應(yīng)該是密文
function md5(str){
? let obj=crypto.createHash('md5');
? obj.update(str);
? return obj.digest('hex')
}
function md5_2(str){
? return md5(md5(str))
}服務(wù)端操作,在用戶名與密碼上添加了簡(jiǎn)單的校驗(yàn),不完善,還需要改進(jìn)
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":"用戶名不能為空"}');
? ? ? ? res.end();
? ? ? }else if(!password){
? ? ? ? res.write('{"err":1,"msg":"密碼不能為空"}');
? ? ? ? res.end();
? ? ? }else if(!/\w{4,16}$/.test(user)){
? ? ? ? res.write('{"err":1,"msg":"用戶名應(yīng)為大小寫字母數(shù)字或下劃線"}');
? ? ? ? res.end();
? ? ? }else if(/['|"]/.test(password)){
? ? ? ? res.write('{"err":1,"msg":"密碼非法"}');
? ? ? ? res.end();
? ? ? }else{
? ? ? ? db.query(`SELECT username FROM users_table WHERE username='${user}'`,(err,data)=>{
? ? ? ? ? if(err){
? ? ? ? ? ? res.write('{"err":1,"msg":"數(shù)據(jù)庫(kù)錯(cuò)誤"}');
? ? ? ? ? ? console.log(err)
? ? ? ? ? ? res.end()
? ? ? ? ? }else{
? ? ? ? ? ? if(data.length>0){
? ? ? ? ? ? ? res.write('{"err":1,"msg":"用戶名已存在"}');
? ? ? ? ? ? ? res.end();
? ? ? ? ? ? }else{
? ? ? ? ? ? ? res.write('{"err":0,"msg":"注冊(cè)成功"}');
? ? ? ? ? ? ? db.query(`INSERT INTO users_table (ID,username,password) VALUES (0,'${user}','${md5_2(password)}')`);
? ? ? ? ? ? ? res.end();
? ? ? ? ? ? }
? ? ? ? ? }
? ? ? ? })
? ? ? }
? ? ? break;
?
? ? case '/login':
? ? ? if(!user){
? ? ? ? res.write('{"err":1,"msg":"用戶名不能為空"}');
? ? ? ? res.end();
? ? ? }else if(!password){
? ? ? ? res.write('{"err":1,"msg":"密碼不能為空"}');
? ? ? ? res.end();
? ? ? }else if(!/\w{4,16}$/.test(user)){
? ? ? ? res.write('{"err":1,"msg":"用戶名應(yīng)為大小寫字母數(shù)字或下劃線"}');
? ? ? ? res.end()
? ? ? }else if(/["|']/.test(password)){
? ? ? ? res.write('{"err":1,"msg":"密碼非法"}');
? ? ? ? res.end();
? ? ? }else{
? ? ? ? db.query(`SELECT username,password FROM users_table WHERE username='${user}'`,(err,data)=>{
? ? ? ? ? if(err){
? ? ? ? ? ? res.write('{"err":1,"msg":"數(shù)據(jù)庫(kù)錯(cuò)誤"}');
? ? ? ? ? ? res.end()
? ? ? ? ? }else if(data.length>0){
? ? ? ? ? ? if(md5_2(password)!=data[0].password){
? ? ? ? ? ? ? res.write('{"err":1,"msg":"用戶名或密碼不正確"}');
? ? ? ? ? ? ? res.end();
? ? ? ? ? ? }else{
? ? ? ? ? ? ? res.write('{"err":0,"msg":"登陸成功"}');
? ? ? ? ? ? ? res.end();
? ? ? ? ? ? }
? ? ? ? ? }else{
? ? ? ? ? ? res.write('{"err":1,"msg":"用戶不存在"}')
? ? ? ? ? }
? ? ? ? })
? ? ? }
? ? 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);以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用nodejs連接mySQL寫接口全過(guò)程(增刪改查)
- nodejs+mysql實(shí)現(xiàn)用戶相關(guān)的增刪改查的詳細(xì)操作
- 三分鐘教會(huì)你用nodejs操作mysql數(shù)據(jù)庫(kù)
- Nodejs?連接?mysql時(shí)報(bào)Error:?Cannot?enqueue?Query?after?fatal?error錯(cuò)誤的處理辦法
- NodeJs操作MYSQL方法詳細(xì)介紹
- nodejs中關(guān)于mysql數(shù)據(jù)庫(kù)的操作
- Nodejs中koa2連接mysql的實(shí)現(xiàn)示例
- nodejs連接mysql數(shù)據(jù)庫(kù)及基本知識(shí)點(diǎn)詳解
- nodejs實(shí)現(xiàn)的連接MySQL數(shù)據(jù)庫(kù)功能示例
- Nodejs連接mysql并實(shí)現(xiàn)增、刪、改、查操作的方法詳解
- NodeJS連接MySQL數(shù)據(jù)庫(kù)并進(jìn)行增刪改查操作詳解
相關(guān)文章
npm報(bào)錯(cuò):npm?WARN?config?global?'--global',?&apo
這篇文章主要給大家介紹了關(guān)于npm報(bào)錯(cuò):npm?WARN?config?global?'--global',?'--local'?are?deprecated.?Use?`--location=global`?instead.的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
nodeJS代碼實(shí)現(xiàn)計(jì)算交社保是否合適
本文通過(guò)nodejs的一個(gè)具體示例來(lái)對(duì)比分析現(xiàn)階段我們交社保合不合適,主要是對(duì)nodejs的一個(gè)小的應(yīng)用,當(dāng)然大家也可以改成其他語(yǔ)言的,程序猿們,來(lái)算算吧。2015-03-03
圖解NodeJS實(shí)現(xiàn)登錄注冊(cè)功能
這篇文章主要介紹了NodeJS實(shí)現(xiàn)登錄注冊(cè)功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Node.js項(xiàng)目中調(diào)用JavaScript的EJS模板庫(kù)的方法
這篇文章主要介紹了Node.js項(xiàng)目中調(diào)用JavaScript的EJS模板庫(kù)的方法,通過(guò)EJS模板引擎可以制作出維護(hù)性良好的HTML代碼結(jié)構(gòu),需要的朋友可以參考下2016-03-03
package-lock.json解決依賴的版本管理使用詳解
這篇文章主要為大家介紹了package-lock.json解決依賴的版本管理使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
node.js中的fs.fstatSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.fstatSync方法使用說(shuō)明,本文介紹了fs.fstatSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12

