文件夾(大體)結構以下:javascript
css:主頁樣式;css
get_pic: 臨時文件存放;html
img:默認頭像路徑;前端
js:首頁的js;java
main:入口server文件(終端執行的);node
node_moudules:下載的模塊和框架;jquery
savePic:保存靜態文件;web
set:註冊,登陸,mongoose初始,郵件發送等模塊;ajax
index:主頁;mongodb
首先,主頁的Html,Css,Js文件:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>Document</title> <script src="./jquery.js"></script> <link rel="stylesheet" href="./css/main.css"> </head> <body> <ul id="list"> <li id="regChange">註冊</li> <li id="loginChange">登陸</li> </ul> <div id="reg"> <input type="file" name="img" id="fileData"> <button id="send">上傳頭像</button> <img id="headImg" src=""> <input type="text" placeholder="郵箱" id="user" /> <input type="text" placeholder="密碼" id="psd" /> <input type="text" placeholder="驗證碼" id="sendmail" /> <button id="email">發送驗證碼</button> <button id="btn">註冊</button> </div> <div id="login"> <input type="text" placeholder="用戶名" id="loguser" /> <input type="text" placeholder="密碼" id="logpsd" /> <button id="logbtn">登陸</button> </div> <script src="./js/main.js"></script> </body> </html>
* { margin: 0; padding: 0; } div:nth-child(3) { display: none; } input { display: block; height: 40px; width: 200px; margin: 20px auto; } button:not(#email) { display: block; height: 30px; width: 70px; background: lightcoral; border: none; margin: 0 auto; } #send{ display: inline-block; } #headImg{ display: block; margin: 0 auto; } #email { display: block; height: 30px; width: 100px; margin: 0 auto; } ul { height: 50px; width: 200px; background: lightblue; margin: 0 auto; list-style: none; } li { height: 50px; width: 100px; float: left; text-align: center; line-height: 50px; } li:hover { background: lightgreen; cursor: pointer; } table{ margin: 50px auto 0; } td{ text-align: center; border: 1px solid lightcoral; } td img{ vertical-align: top; }
//主頁js文件 //增長監聽事件 btn.addEventListener("click", clickHandler); //註冊 logbtn.addEventListener("click", clickHandler); //登陸 email.addEventListener("click", sendHandler); //發送驗證碼 regChange.addEventListener("click", changeHandler); //切換登陸註冊 loginChange.addEventListener("click", changeHandler); $('#send').on('click', sendHead); webAdd = 'http://localhost:1024/main'; var table; //新建登錄成功後的表格 function sendHead(e) { //上傳文件 var picData = new FormData(); //實例化文件對象 picData.append("sendImg", $("#fileData")[0].files[0]); //將上傳文件的添加到文件中 $.ajax({ //ajax方式上傳至後臺 url: webAdd + '/head', type: 'POST', data: picData, cache: false, contentType: false, processData: false, success: function (data) { if (!data.error) { alert('Success'); headImg.src = data.data; //成功時將圖片顯示 headImg.style.width = '100px'; } }, error: function () { alert('Error'); } }); } function clickHandler(e) { if (this.textContent === "註冊") { //若爲空時跳出 if (!user.value || !psd.value || !sendmail.value) { alert("輸入不能爲空"); return; } var AllData = { email: user.value, password: psd.value, mailnum: sendmail.value, headPic: headImg.src }; //點擊註冊時將郵箱號,密碼,驗證碼,頭像傳送至後臺 $.post(webAdd + '/reg', AllData, function (res) { //後臺返回的對象,若hasUser爲真,說明已有用戶名,不然註冊成功 if (res.hasUser) { // location.reload(); alert("註冊失敗"); return; } else { alert("註冊成功~"); } //成功後隱藏註冊,顯示登陸 reg.style.display = "none"; login.style.display = "block"; } ); } else if (this.textContent === "登陸") { // 同註冊,不能爲空 if (!loguser.value || !logpsd.value) { alert("不能爲空"); return; } //點擊登陸時將郵箱號,密碼傳送至後臺 $.post(webAdd + '/login', { email: loguser.value, password: logpsd.value }, function (res) { //後臺返回的對象,若isUser爲真,說明正確,並跳轉至歡迎頁,不然失敗 if (res.isUser) { alert("登陸成功"); login.style.display = "none"; list.style.display = 'none'; createTab(document.body, res.data[0]); //若登錄成功,建立用戶表格 } else { alert("用戶名或密碼不正確"); return; } } ); } } function sendHandler(e) { // 點擊獲取驗證碼後將驗證碼發送到後端進行比對 $.post(webAdd + '/sendmail', { email: user.value }); } function changeHandler(e) { // 點擊上方的註冊登陸切換 if (e.target.textContent === "註冊") { reg.style.display = "block"; login.style.display = "none"; } else { reg.style.display = "none"; login.style.display = "block"; } } function createTab(parent, data) {//新建表格函數 if (table) { table.remove();//若表格存在,刪除表格 } table = document.createElement('table'); for (var str in data) {//根據遍歷數據建立表格 var tr = document.createElement('tr'); var td = document.createElement('td'); if (str === 'head') { td.textContent = '頭像:' var img = new Image(); img.src = data[str]; td.appendChild(img); } else { td.textContent = str + ':' + data[str]; } tr.appendChild(td); table.appendChild(tr); } parent.appendChild(table); }
以後是其餘模塊:
const mongoose = require('./main'); //引入main模塊 const Schema = mongoose.Schema; //新建schema對象 let userSchema = new Schema({ email: { type: String, required: true }, password: { type: String, required: true }, head: { type: String, required: true } }); //實例化對象 let userModel = mongoose.model('allUser', userSchema); //新建數據庫 module.exports = userModel; //拋出模塊
const mongoose = require('mongoose');//引入mongoose mongoose.connect('mongodb://localhost:27017/UserList', { useNewUrlParser: true });//鏈接數據庫 let db = mongoose.connection; db.on("error", function (error) { console.log("錯誤:" + error); }); db.on("open", function () { console.log("鏈接成功"); }); db.on('disconnected', function () { console.log('鏈接斷開'); }); module.exports = mongoose;//拋出mongose對象
const nodemailer = require("nodemailer"); let obj = { transporter: nodemailer.createTransport({ service: "qq", // 運營商 qq郵箱 網易// port: 465, secure: true, auth: { user: "********@qq.com", //發送方的郵箱 pass: "**********" // pop3 受權碼 } }), send: function(mail, content,callback) { mailOptions = { from: '"Hello World~" <**********@qq.com>', to: mail, subject: content, text: content, html: "<h1>" + content + "</h1>" }; this.transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log("Message sent: %s", info.messageId); callback(); }); } }; module.exports = obj;
const express = require('express'); const router = express.Router(); //新建路由 var multer = require('multer'); //文件獲取儲存的第三方模塊 const fs = require('fs'); const path = require('path'); var upload = multer({ dest: '../get_pic/' }); //將頭像臨時文件夾 router.post('/head', upload.single('sendImg'), (req, res) => { //路由地址 //讀取傳輸的頭像 fs.readFile(req.file.path, (err, data) => { if (err) { throw ('Load_Err'); } let type = req.file.mimetype.split('/')[1]; //獲取文件類型名 let name = new Date().getTime() + parseInt(Math.random() * Math.random() * 1000000); //使用時間戳和隨機數生成隨機名,而且連成完整的文件名 //保存文件至savePic文件夾 let filename = name + '.' + type; fs.writeFile(path.join(__dirname, '../savePic/' + filename), data, (err) => { // 返回信息給前端 if (err) { res.send({ err: 1, msg: '上傳失敗' }); //保存圖片後刪除臨時文件 fs.unlink(req.file.path, (err) => { if (err) { console.log('刪除失敗'); } }); return; } res.send({ err: 0, msg: '上傳成功', data: 'savePic/' + filename }); //保存圖片後刪除臨時文件 fs.unlink(req.file.path, (err) => { if (err) { console.log('刪除失敗'); } }); }); }); }); module.exports = router;
const express = require('express'); const router = express.Router(); //新建路由 const Model = require('./mod'); //傳入數據庫對象 const fs = require('fs'); const path = require('path'); const sendMail = require('./send'); //傳入郵件發送的模塊對象 var count = ""; //新建一個空字符存放驗證碼,可供全局調用 //用post方法傳輸數據 router.post('/reg', (req, res) => { //保存前端傳來的數據 var mail = req.body.email; var psd = req.body.password; var mailnum = req.body.mailnum; var headUrl = req.body.headPic; //使用Model對象鏈接數據庫 Model.find({ // 查詢郵箱 'email': mail }).then((data) => { //若返回的郵箱找到或驗證碼不對,則拋錯 if (data.length >= 1 || mailnum !== count) { res.send({ hasUser: true }); return; } res.send({ hasUser: false }); //若前端傳了圖片,則保存上傳的圖片,不然使用默認圖片 if (headUrl==='http://localhost:1024/') { headUrl = 'http://localhost:1024/img/cat.gif'; } Model.insertMany({ //反之將郵箱插入到數據庫 'email': mail, 'password': psd, 'head':headUrl }).then((result) => { console.log(result); }) }).catch((err) => { console.log(err); }); }); //郵箱驗證接口 router.post("/sendmail", (req, res) => { count = ""; //初始化驗證碼容器 var mail = req.body.email; //獲取前端傳來的郵箱號 for (let i = 0; i < 4; i++) { count += Math.floor(Math.random() * 10); //生成4個隨機數 } var callback = () => { console.log("發送成功"); }; sendMail.send(mail, count, callback); //調用郵件發送模塊 res.send('send'); }); module.exports = router;
const express = require('express'); const router = express.Router();//新建路由 const Model = require('./mod');//獲取數據庫對象 router.post('/login', (req, res) => {//路由地址 //保存前端傳來的數據 var mail = req.body.email; var psd = req.body.password; Model.find({ // 查詢是否有該用戶,若用戶郵箱和密碼符合,則拋出正確,不然拋出錯誤對象 'email': mail, 'password': psd }).then((data) => { if (data.length >= 1) { res.send({ isUser: true, data:data }); } else { res.send({ isUser: false }); } }).catch((err) => { console.log(err); }); }); module.exports = router;
const express = require('express'); const app = express(); const cors = require("cors"); //引入cors模塊(解決跨域問題) const path = require('path'); var bodyParser = require('body-parser'); app.use(cors()); // 下面的相似於http請求的頭文件(另外一篇文章有寫到http請求,也是註冊登陸) app.all("*", function (req, res, next) { //設置容許跨域的域名,*表明容許任意域名跨域 res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "content-type"); //容許的header類型 res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS"); //跨域容許的請求方式 next(); //是否繼續向下執行 }); //建立靜態文件目錄 app.use(express.static(path.join(__dirname, '../../user_info'))); //解決post傳輸的數據格式問題 app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) // 路由選擇導入註冊 const reg = require('../set/reg.js'); app.use('/main', reg); // 路由選擇導入登陸 const login = require('../set/login.js'); app.use('/main', login); // 路由選擇上傳圖片模塊 const head = require('../set/sendhead.js'); app.use('/main', head); // 開啓監聽 app.listen(1024, () => { console.log('Server Start~'); });