代码拉取完成,页面将自动刷新
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);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。