咱們遇到的系統基本都少不了註冊和登陸功能,今天咱們就來簡單的說下這個過程。 個人是以node做爲後臺,接受前端的請求數據返回數據給前端,前端在這一入門系列中。因爲前端瀏覽器請求有跨域問題,這裏我用nginx配置了一下代碼。 html
前端訪問請求統一在一個index.html文件中(ps,你能夠用postman或postwomen)發請求,就不用寫這index.html和nginx配置。首先你要註冊,這時候就須要有一張用戶表來保存註冊的數據。咱們簡單的就須要姓名,密碼,手機號,這三個字段。對於註冊和登陸中須要驗證,加密,解密之類的,這裏就不說了。用本地的Mysql新建t_user表。 前端
class Response {
constructor(isSuc, msg, code, result) {
this.isSuc = isSuc;
this.msg = msg;
this.code = code;
this.result = result;
}
}
module.exports = Response;
複製代碼
var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function register(req, res) {
//打印請求報文
var param = req.body;
var username = param.username;
var password = param.password;
var phone = param.phone;
var response = new Response(false, '', -1);
if (username && password && phone) {
//一、查看數據庫中是否有相同用戶名
connection.query("select * from t_user where username = ?", [username], function (error, results, fields) {
if (error) throw error;
if (results.length >= 1) {
//二、若是有相同用戶名,則註冊失敗,用戶名重複
response = new Response(false, '註冊失敗,用戶名重複', -1);
//打印響應報文
logger.info(response);
res.send(response);
} else {
connection.query("insert into t_user(username,password,phone) VALUES(?,?,?)", [username, password, phone], function (error, results, fields) {
if (error) throw error;
//三、若是沒有相同用戶名,而且有一條記錄,則註冊成功
if (results.affectedRows == 1) {
response = new Response(false, '註冊成功', 1);
logger.info(response);
res.send(response);
} else {
response = new Response(false, '註冊失敗', -1);
logger.info(response);
res.send(response);
}
});
}
})
} else {
response = new Response(false, '註冊失敗,用戶名、密碼、用戶號不能爲空', -1);
logger.info(response);
res.send(response);
}
}
module.exports = register;
複製代碼
var logger = require('../logConfig');
var connection = require('../sqlConfig');
var Response = require('./response');
function login(req, res) {
//打印請求報文
var param = req.body;
var username = param.username;
var password = param.password;
var response = new Response(false, '', -1);
if (username && password) {
//一、查詢數據庫中是否有用戶名
connection.query("select * from t_user where username = ?", [username], function (error, results, fields) {
if (error) throw error;
if (results.length >= 1) {
//二、若是有用戶名,查詢密碼是否相同
if (password == results[0].password) {
//三、密碼相同則登錄成功
response = new Response(true, '登錄成功', 1);
logger.info(response);
res.send(response);
} else {
response = new Response(true, '登錄失敗,密碼錯誤', -1);
logger.info(response);
res.send(response);
}
} else {
response = new Response(false, '登錄失敗,沒有此用戶名', -1);
logger.info(response);
res.send(response);
}
});
} else {
response = new Response(false, '登錄失敗,用戶名或密碼不能爲空', -1);
//打印響應報文
logger.info(response);
res.send(response);
}
}
module.exports = login;
複製代碼
var express = require('express');
var app = express();
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: false }))
//設置跨域訪問
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
var register = require('./api/register'); // 註冊
var login = require('./api/login'); // 登陸
app.post('/register', (req, res) => register(req, res));
app.post('/login', (req, res) => login(req, res));
// 咱們能夠uncaughtException來全局捕獲未捕獲的Error,同時你還能夠將此函數的調用棧打印出來,捕獲以後能夠有效防止node進程退出
process.on('uncaughtException', function (err) {
//打印出錯誤
console.log(err);
//打印出錯誤的調用棧方便調試
console.log(err.stack);
});
// connection.end();
app.listen(3000, function () { ////監聽3000端口
console.log('Server running main.js at 3000 port');
});
複製代碼
再文章開頭Nginx配置那裏已經把註冊接口顯示出來了,接下來就看登陸了。 node
咱們看看數據庫是否有了 咱們的登陸和註冊就完成了哈。是否是以爲很簡單呢??透漏一下下期內容,就是商品的增刪改查了。