Ajax code of the front end:
/**
* Created by Administrator on 2018/4/17.
*/
$(function() {
var $loginBox = $("-sharploginBox");
var $registerBox = $("-sharpregisterBox");
var $userInfo = $("-sharpuserInfo");
//
$loginBox.find("a.colMint").on("click", function() {
$registerBox.show();
$loginBox.hide();
});
//
$registerBox.find("a.colMint").on("click", function() {
$loginBox.show();
$registerBox.hide();
});
//
$registerBox.find("button").on("click", function() {
//ajax
$.ajax({
type: "post",
url: "/api/user/register",
data: {
username: $registerBox.find("[name="username"]").val(),
password: $registerBox.find("[name="password"]").val(),
repassword: $registerBox.find("[name="repassword"]").val()
},
dataType: "json",
success: function(result) {
$registerBox.find(".colWarning").html(result.message);
if (!result.code) {
//
setTimeout(function() {
$loginBox.show();
$registerBox.hide();
}, 1000);
}
}
});
});
//
$loginBox.find("button").on("click", function() {
//ajax
$.ajax({
type: "post",
url: "/api/user/login",
data: {
username: $loginBox.find("[name="username"]").val(),
password: $loginBox.find("[name="password"]").val()
},
dataType: "json",
success: function(result) {
$loginBox.find(".colWarning").html(result.message);
if (!result.code) {
//
setTimeout(function() {
window.location.reload();
}, 1000);
}
}
})
})
//
$("-sharplogout").on("click", function() {
$.ajax({
url: "/api/user/logout",
success: function(result) {
if (!result.code) {
window.location.reload();
}
}
});
})
})
app.js of backend
/**
* Created by Administrator on 2018/4/16.
*
*/
//express
var express = require("express");
//
var swig = require("swig");
//
var mongoose = require("mongoose");
//body-parserpost
var bodyParser = require("body-parser");
//cookies
var cookieParser = require("cookie-parser")
//app=>NodeHS Http.createServer()
var app = express();
//
//url/public__dirname + "/public"
app.use("/public", express.static(__dirname + "/public"));
// app.use("/static", express.static("public"));
//
//
//
app.engine("html", swig.renderFile);
//views
app.set("views", "./views");
/*view engineapp.engine*/
app.set("view engine", "html");
//
swig.setDefaults({cache: false});
//bodyparser
app.use( bodyParser.urlencoded({extended:true}) );
app.use( function(req, res, next) {
req.cookies = new cookieParser(req, res);
//cookie
req.userInfo = {};
if (req.cookies.get("userInfo")) {
try {
req.userInfo = JSON.parse(req.cookies.get("userInfo"));
//
User.findById(req.userInfo._id).then(function(userInfo) {
req.userInfo.isAdmin = Boolean(userInfo.isAdmin);
next();
})
}catch(e){
next();
}
} else {
next();
}
} );
/*
*
* */
app.use("/admin", require("./routers/admin"));
app.use("/api", require("./routers/api"));
app.use("/", require("./routers/main"));
//http
mongoose.connect("mongodb://localhost:27017/blog",function (err) {
if(err){
console.log("");
}else{
console.log("");
app.listen(8081);
}
});
// app.listen(8081);
api.js
/**
* Created by Administrator on 2018/4/16.
*/
var express = require("express");
var router = express.Router();
var User = require("../models/User");
//
var responseData;
router.use(function (req, res, next) {
responseData = {
code: 0,
message:""
}
next();
});
/*
*
*
*
* 1.
* 2.
* 3.
*
* 1.
*
* */
router.post("/user/register", function (req, res, next) {
var username = req.body.username;
var password = req.body.password;
var repassword = req.body.repassword;
//
if(username == ""){
responseData.code = 1;
responseData.message = "";
res.json(responseData);
return;
}
//
if(password == ""){
responseData.code = 2;
responseData.message = "";
res.json(responseData);
return;
}
//
if(password != repassword){
responseData.code = 3;
responseData.message = "";
res.json(responseData);
return;
}
//
User.findOne({
username: username
}).then(function (userInfo) {
if( userInfo){
//
responseData.code = 4;
responseData.message = ""
res.json(responseData);
return;
}
//
var user = new User({
username: username,
password: password
});
return user.save();
}).then(function (newUserInfo) {
responseData.message = "";
res.json(responseData);
});
});
router.post("/user/login", function (req, res, next) {
var username = req.body.username;
var password = req.body.password;
if( username == ""|| password ==""){
responseData.code = 1;
responseData.message = "";
res.json(responseData);
return;
}
//
User.findOne({
username: username,
password: password
}).then(function (userInfo) {
if(!userInfo){
responseData.code = 2;
responseData.message = "";
res.json(responseData);
return;
}
//
responseData.message = "";
responseData.userInfo = {
_id: userInfo._id,
username: userInfo.username
}
req.cookies.set("userInfo", JSON.stringify({
_id: userInfo._id,
username: userInfo.username
}));
res.json(responseData);
return;
})
});
/*
*
* */
router.get("/user/logout", function(req, res) {
req.cookies.set("userInfo", null);
res.json(responseData);
});
module.exports = router;
error reporting problem:
TypeError: req.cookies.get is not a function
at D:\WorkSpace\WebStorm\project\app.js:42:21
at Layer.handle [as handle_request] (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:317:13)
at D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:335:12)
at next (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (D:\WorkSpace\WebStorm\project\node_modules\body-parser\lib\types\urlencoded.js:91:7)
at Layer.handle [as handle_request] (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:317:13)
at D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:335:12)
at next (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:275:10)
at expressInit (D:\WorkSpace\WebStorm\project\node_modules\express\lib\middleware\init.js:40:5)
at Layer.handle [as handle_request] (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:317:13)
at D:\WorkSpace\WebStorm\project\node_modules\express\lib\router\index.js:284:7