在初次接觸ajax后,我們做了一個crm訓(xùn)練的項目,大多數(shù)小組都有注冊用戶這一項,但是都忽略掉了一個功能,那就是,很多網(wǎng)站的注冊是可以上傳頭像的,在這里我做了一個在已有的頭像數(shù)組里選擇圖片上傳作頭像的小型crm(當然,可以從本地照片上傳并裁剪的那種我還沒做出來,不過只要有時間我就會研究的,相信時間也不會太久的)。
1.先寫出一個注冊頁面以及css樣式我命名為regist.html,css文件名為regist.css,在這里我省略掉具體代碼,上圖看效果吧:(頁面有點丑,別介意)
還有一個用于顯示添加后記錄的information.html頁面,此時只有表頭:
2.寫出創(chuàng)建連接池模塊(dbutil.js),也即是建立鏈接的js文件,我在這里建的是users_infor表,使用的數(shù)據(jù)庫是test。
var mysql = require('mysql');var pool = mysql.createPool({host : 'localhost',user : 'root',password : 'lovo',database:"test",port:3306});exports.pool=pool;
3.寫出模塊用來連接數(shù)據(jù)庫,處理(增刪改查)用戶數(shù)據(jù)(Userdao.js),里面把操作數(shù)據(jù)庫的函數(shù)一律命名為getAllUser:
var db = require("../DBUtil/dbutil.js");//var conn = db.conn;var mypool =db.pool;function getAllUser(sql,arg,fun){mypool.getConnection(function(err,conn){conn.query(sql,arg,fun);conn.end();})}exports.getAllUser=getAllUser;
4.寫出操作數(shù)據(jù)庫的模塊,也即是對數(shù)據(jù)表的增,刪,改,查(Userservice.js):
var dao = require("../dao/UserDao.js");
定義注冊函數(shù),即往數(shù)據(jù)表user_infor添加新紀錄的函數(shù)
exports.regist = function(req,res){var arg;if (req.method == "get" || req.method == "GET") {arg = [req.query.username, req.query.pwd, req.query.pics];} else {arg = [req.body.username, req.body.pwd, req.body.pics];}var sql = "insert into user_infor(u_name,u_pwd,u_pics) values(?,?,?)"dao.getAllUser(sql, arg, function (err, result) {if (err) {console.log(err);} else {if (result.affectedRows>0){res.sendfile("./static/html/information.html")} else {res.sendfile("./static/html/regist.html")}}})}
定義顯示information.html頁面所有記錄的函數(shù),即查詢user_infor表所有內(nèi)容的函數(shù)
exports.listAll=function(req,res){var sql = " select * from user_infor ";dao.getAllUser(sql,function (err, result, fields) {if (err){console.log(err);} else {if (result.length>0){res.json(result);console.log(result)} else {res.send("failed");}}})}
5.當然,不要忘了引入2個模塊express和mysql,新建一個文件夾node_module,將這2個模塊包含在其中。
新聞熱點
疑難解答
圖片精選