欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NodeJs+MySQL實現(xiàn)注冊登錄功能

 更新時間:2022年04月26日 15:55:55   作者:我是一只喵~  
這篇文章主要為大家詳細介紹了NodeJs+MySQL實現(xiàn)注冊登錄功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了NodeJs+MySQL實現(xiàn)注冊登錄功能的具體代碼,供大家參考,具體內(nèi)容如下

之前寫過一個沒有連接數(shù)據(jù)庫的注冊與登陸的實現(xiàn),這次加上了數(shù)據(jù)庫

剛剛接觸后端,很多不完善的地方,有錯誤請指正

nodejs中mysql的寫法:

//連接池
let db=mysql.createPool({'配置'}) ??
db.query(`sql語句`,(err,data)=>{})

并且在數(shù)據(jù)庫中存儲的密碼應(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ù)端操作,在用戶名與密碼上添加了簡單的校驗,不完善,還需要改進

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ù)庫錯誤"}');
? ? ? ? ? ? console.log(err)
? ? ? ? ? ? res.end()
? ? ? ? ? }else{
? ? ? ? ? ? if(data.length>0){
? ? ? ? ? ? ? res.write('{"err":1,"msg":"用戶名已存在"}');
? ? ? ? ? ? ? res.end();
? ? ? ? ? ? }else{
? ? ? ? ? ? ? res.write('{"err":0,"msg":"注冊成功"}');
? ? ? ? ? ? ? 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ù)庫錯誤"}');
? ? ? ? ? ? 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);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • npm報錯:npm?WARN?config?global?'--global',?'--local'?are?deprecated解決

    npm報錯:npm?WARN?config?global?'--global',?&apo

    這篇文章主要給大家介紹了關(guān)于npm報錯:npm?WARN?config?global?'--global',?'--local'?are?deprecated.?Use?`--location=global`?instead.的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • nodeJS代碼實現(xiàn)計算交社保是否合適

    nodeJS代碼實現(xiàn)計算交社保是否合適

    本文通過nodejs的一個具體示例來對比分析現(xiàn)階段我們交社保合不合適,主要是對nodejs的一個小的應(yīng)用,當然大家也可以改成其他語言的,程序猿們,來算算吧。
    2015-03-03
  • 圖解NodeJS實現(xiàn)登錄注冊功能

    圖解NodeJS實現(xiàn)登錄注冊功能

    這篇文章主要介紹了NodeJS實現(xiàn)登錄注冊功能,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Node.js項目中調(diào)用JavaScript的EJS模板庫的方法

    Node.js項目中調(diào)用JavaScript的EJS模板庫的方法

    這篇文章主要介紹了Node.js項目中調(diào)用JavaScript的EJS模板庫的方法,通過EJS模板引擎可以制作出維護性良好的HTML代碼結(jié)構(gòu),需要的朋友可以參考下
    2016-03-03
  • package-lock.json解決依賴的版本管理使用詳解

    package-lock.json解決依賴的版本管理使用詳解

    這篇文章主要為大家介紹了package-lock.json解決依賴的版本管理使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • 使用npm安裝最新版本nodejs

    使用npm安裝最新版本nodejs

    本文給大家分享的是如何使用npm安裝最新版本nodejs的方法,主要用到了nodejs版本管理模塊n,非常的好用,有需要的小伙伴可以參考下
    2018-01-01
  • node.js中的fs.fstatSync方法使用說明

    node.js中的fs.fstatSync方法使用說明

    這篇文章主要介紹了node.js中的fs.fstatSync方法使用說明,本文介紹了fs.fstatSync的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • npm國內(nèi)鏡像 安裝失敗的幾種解決方案

    npm國內(nèi)鏡像 安裝失敗的幾種解決方案

    這篇文章主要給大家總結(jié)了npm國內(nèi)鏡像npm安裝失敗的幾種解決方案,文中介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下來一起看看吧。
    2017-06-06
  • Nest.js散列與加密實例詳解

    Nest.js散列與加密實例詳解

    這篇文章主要給大家介紹了關(guān)于Nest.js散列與加密的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Nodejs之http的表單提交

    Nodejs之http的表單提交

    這篇文章主要為大家詳細介紹了Nodejs之http的表單提交,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論