1 Star 0 Fork 0

Laowang/商品后台管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 5.18 KB
一键复制 编辑 原始数据 按行查看 历史
Laowang 提交于 2019-04-06 22:48 . 完毕
const express = require('express');
const bodyParse = require('body-parser');
const mdb = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const session = require('express-session');
const MD5 = require('md5-node');
const multiparty = require('multiparty')
const app = new express();
const mdbUrl = "mongodb://localhost:27017/";
const mdbname = "shop";
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use('/upload',express.static('upload'));
app.use(bodyParse.urlencoded({ extended: false }));
app.use(bodyParse.json());
app.use(session({
secret: 'sessionIds',
resave: false,
saveUninitialized: true,
cookie: { maxAge: 360000 },
rolling: true
}))
app.locals['username'] = "";
app.use(function (req, res, next) {
if (req.url != '/login' && req.url != '/dologin') {
if (req.session.userinfo && req.session.userinfo.username != '') {
next();
} else {
res.redirect('/login');
}
} else {
next();
}
});
app.get('/', function (req, res) {
res.redirect('/index');
});
app.get('/index', function (req, res) {
mdb.connect(mdbUrl, { useNewUrlParser: true }, function (err, client) {
if (err) {
res.status(404);
res.send("err");
} else {
var db = client.db(mdbname);
var result = db.collection('goods').find();
result.toArray(function (error, data) {
if (error) {
console.log(error);
return;
}
client.close();
res.render('index', {
"goods": data
});
})
}
})
});
app.get('/login', function (req, res) {
res.render('login');
});
app.post("/dologin", function (req, res) {
mdb.connect(mdbUrl, { useNewUrlParser: true }, function (err, client) {
if (err) {
res.status(404);
res.send("err");
} else {
var db = client.db(mdbname);
req.body.password = MD5(req.body.password);
var result = db.collection('user').find(req.body);
result.toArray(function (err, data) {
if (data.length > 0) {
req.session.userinfo = data[0];
app.locals['username'] = data[0].username;
console.log("登录成功!");
res.redirect("/index");
} else {
console.log("登录失败!");
res.send("<script>alert('用户不存在或帐号密码错误!');location.href='/login'</script>");
}
});
client.close();
}
})
})
app.get('/add', function (req, res) {
res.render('add');
});
app.post('/doAdd', function (req, res) {
var form = new multiparty.Form();
form.uploadDir = 'upload'
form.parse(req, function (err, fields, files) {
//获取提交的数据以及图片上传成功返回的图片信息
var title = fields.title[0];
var price = fields.price[0];
var fee = fields.fee[0];
var description = fields.description[0];
var pic = files.pic[0].path;
mdb.connect(mdbUrl, { useNewUrlParser: true }, function (err, client) {
if (err) {
res.status(404);
res.send("err");
} else {
var db = client.db(mdbname);
const good = db.collection('goods');
good.insertMany([{
name:title,
price,
fee,
description,
pic
}],function(err,data) {
client.close();
if(!err){
res.redirect('/index');
}else{
console.log("上传失败!" + err);
res.send("<script>alert('上传失败!');location.href='/add'</script>");
}
});
}
})
});
});
app.get('/edit', function (req, res) {
res.render('edit');
});
app.get('/outlogin', function (req, res) {
req.session.destroy(function (err) {
if (err) {
console.log(err);
res.send('退出失败');
}
app.locals['username'] = '';
res.redirect('/login');
});
});
app.get('/del',function(req,res){
var id = req.query.id;
mdb.connect(mdbUrl, { useNewUrlParser: true }, function (err, client) {
if (err) {
res.status(404);
res.send("err");
} else {
var db = client.db(mdbname);
const good = db.collection('goods');
good.deleteOne({"_id":new ObjectID(id)},function(err,result){
client.close();
if(!err){
res.send("<script>alert('删除成功!');location.href='./index'</script>");
}else{
res.send("<script>alert('删除失败!');location.href='./index'</script>");
}
})
}
})
});
app.listen(8088);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/laowangHub/nodeshops.git
git@gitee.com:laowangHub/nodeshops.git
laowangHub
nodeshops
商品后台管理系统
master

搜索帮助