1創(chuàng)建連接池對象
成都創(chuàng)新互聯(lián)公司服務(wù)項目包括新興網(wǎng)站建設(shè)、新興網(wǎng)站制作、新興網(wǎng)頁制作以及新興網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,新興網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到新興省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
2導(dǎo)出連接池對象
/** * 1.引入MySQL模塊 * 2.創(chuàng)建連接池對象 * 3.導(dǎo)出連接池對象 */ const mysql = require('mysql'); var pool = mysql.createPool({ host:'localhost', port:'3306', user:'xxx', password:'xxx', database:'xxx', connectionLimit:20 }); module.exports = pool;
1托管靜態(tài)資源到public
2使用body-parser中間件
3使用路由器掛在到指定的位置
//引入express模塊 const express = require('express'); //引入路由器 const userRouter = require('./routes/user.js'); const productRouter = require('./routes/product.js'); const myproRouter = require('./routes/mypro.js'); const demoRouter = require('./routes/demo.js'); const bodyParser = require('body-parser'); //創(chuàng)建web服務(wù)器 var server = express(); //監(jiān)聽端口 server.listen(8080); //托管靜態(tài)資源 server.use(express.static('public')); server.use(express.static('ajaxdemo')); server.use(express.static('mypro')); server.use(express.static('js')); server.use(express.static('css')); server.use(express.static('bootstrap')); server.use(express.static('img')); //使用body-parser中間件 server.use(bodyParser.urlencoded({ extended:false })); //掛載路由器 server.use('/user',userRouter); server.use('/demo',demoRouter);
1引入連接池模塊
2創(chuàng)建路由器對象
3往路由器中添加路由
4在路由中使用連接池
5導(dǎo)出路由器
/* 1.引入express 2.創(chuàng)建路由器對象 3.添加路由 4.導(dǎo)出路由器 5.引入連接池對象 6.將數(shù)據(jù)插入到數(shù)據(jù)庫中 */ const express = require('express'); const pool = require('../pool.js'); var router = express.Router(); //查看所有數(shù)據(jù) router.get('/sele', (req, res) => { //驗證數(shù)據(jù)是否為空 var obj = req.query; //console.log('query',obj); for(var key in obj) { if(!obj[key]) { res.send('數(shù)據(jù)不能為空'); return; } } var sqlselect = 'select * from xxx'; pool.query(sqlselect,(err, result) => { if(err) throw err; if(result.length > 0) { res.send(result); } }); }); //查看用戶名 router.get('/seleUname', (req, res) => { //驗證數(shù)據(jù)是否為空 var obj = req.query; //console.log('query',obj); for(var key in obj) { if(!obj[key]) { res.send('數(shù)據(jù)不能為空'); return; } } var sqlselect = 'select uname from xxx where uname = ?'; pool.query(sqlselect,[obj.uname],(err,result) => { if(err) throw err; if(result.length > 0) { console.log(result.tength); res.send('1'); }else{ res.send('0'); } }); }); router.post('/reg', (req, res) => { var obj = req.body; console.log('body',obj); for(var key in obj){ if(!obj[key]){ res.send('內(nèi)容不能為空'); return; } } var selectInsert = 'insert into xxx set ?'; pool.query(selectInsert, [obj], (err, result) => { if(err) throw err; if(result.affectedRows > 0) { console.log(result.affectedRows) res.send('1'); } else { res.send('0'); console.log(result.affectedRows) } }); }); //導(dǎo)出路由器 module.exports = router;
4.html頁面
Document
5.js前端驗證以及Ajax異步交互實現(xiàn)用戶注冊
window.onload = function() { uname.onfocus = notNull; uname.onblur = notNull; upwd.onfocus = notNull; upwd.onblur = notNull; upwd1.onfocus = notNull; upwd1.onblur = notNull; email.onfocus = notNull; email.onblur = notNull; phone.onfocus = notNull; phone.onblur = notNull; upwd.onfocus = passw; upwd.onblur = passw; upwd1.onfocus = passw; upwd1.onblur = passw; //聲明一個全局的xhr var xhr = new XMLHttpRequest(); var flag = true; //驗證是否為空并且用戶名是否已經(jīng)存在 function notNull() { if(!uname.value) { p1.innerHTML = '用戶名不能為空'; return; } else { p1.innerHTML = ''; getUname(); } if(!upwd.value) { p2.innerHTML = '密碼不能為空'; return; } else { p2.innerHTML = ''; } if(!upwd1.value) { p3.innerHTML = '確認密碼不能為空'; return; } else { p3.innerHTML = ''; } if(!email.value) { p4.innerHTML = '郵箱不能為空'; return; } else { p4.innerHTML = ''; } if(!phone.value) { p5.innerHTML = '手機號不能為空'; return; } else { p5.innerHTML = ''; } } //驗證用戶名是否已存在 function getUname() { xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; console.log(result); if(result === '1') { p1.innerHTML = '用戶名已存在'; //如果用戶名已存在,該按鈕處于禁用狀態(tài) reg.setAttribute('disabled','true'); } else { p1.innerHTML = ''; reg.disabled = false; } } } var url = "/demo/seleUname?uname=" + uname.value; xhr.open('get', url, true); xhr.send(null); } //密碼驗證 function passw() { if(upwd.value != upwd1.value) { p3.innerHTML = '兩次密碼不一致'; } } reg.onclick = function() { //查詢所有用戶信息 xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; console.log(JSON.parse(result)); } } var url = "/demo/sele"; xhr.open('get', url, true); xhr.send(null); //執(zhí)行注冊 xhr.onreadystatechange = function() { if(xhr.readyState == 4 && xhr.status == 200) { var result = xhr.responseText; if(flag) { getUname(); if(result === '1') { alert('success'); setTimeout(()=>{ location.href = 'http://localhost:8080/login_user.html'; }); } else { alert('error'); } } } } var str = "/demo/reg"; xhr.open('post', str, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var formdata = "uname=" + uname.value + "&upwd=" + upwd.value + "&email=" + email.value + ""; xhr.send(formdata); } }
界面展示:
總結(jié)
以上所述是小編給大家介紹的Nodejs實現(xiàn)用戶注冊功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!