// server.js
// set up ======================================================================
// get all the tools we need
var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var path = require('path');
var bodyParser = require('body-parser');
var credentials = require('./credentials.js');
var session = require('express-session');
// set up handlebars view engine
app.set('views',path.join(__dirname,'public/views'));
app.set('view engine', 'hbs');
// setup
app.use(express.static(path.join(__dirname, 'public/assets')));
//db config
var configDB = require('./config/db.js');
mongoose.connect(configDB.url); // connect to our database
//mongoose session setup
const MongoStore = require('connect-mongo')(session);
var options = {
server: {
socketOptions: { keepAlive: 1 }
}
};
//body parser and session and cookie parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(require('cookie-parser')(credentials.cookieSecret));
app.use(require('express-session')({
resave: false,
saveUninitialized: false,
secret: 'iamsupersecret',
store: new MongoStore({url:configDB.url,options})
}));
// flash message middleware
app.use(function(req, res, next)
{
res.locals.flash = req.session.flash;
delete req.session.flash;
next();
});
// routes ======================================================================
require('./routes.js')(app);
// 404 catch-all handler (middleware)
app.use(function(req, res, next)
{
res.status(404);
res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next)
{
console.error(err.stack);
res.status(500);
res.render('500');
});
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);
我正在构建一个购买门票的小应用程序,现在我在我的mongodb中有一个购物车集合,使用会话ID保存用户购物车。我希望以类似会话的方式保存数据,这样一旦用户结束会话,购物车就会被删除,我不再需要数据库,因为我不需要购物车数据,因为我有我需要的所有物流的销售数据。下面是我的购物车控制器,显示我目前如何将数据保存到我的购物车集合。
exports.addItem = function (req, res)
{
if(!userKey)
{
userKey = req.session.id;
}
Cart.findOne({userKey : userKey},function (err, cart)
{
if(err)
{
console.log("Error finding cart "+err);
req.session.flash =
{
type: 'danger',
intro: 'Ooops!',
message: 'There was an error finding your cart.'
};
return res.redirect(303, '/cart');
}
else if(!cart)
{
cart = new Cart();
cart.userKey = userKey;
cart.items = [];
Ticket.findOne({_id:req.params.id},function (err, ticket)
{
if(err)
{
console.log("Error finding ticket "+err);
req.session.flash =
{
type: 'danger',
intro: 'Ooops!',
message: 'There was an error finding that ticket.'
};
return res.redirect(303, '/cart');
}
var item = {
id:ticket._id,
type: ticket.type,
price: ticket.price,
quantity: 1,
time: ticket.time,
date: ticket.date
};
cart.items.push(item);
cart.save();
return res.redirect(303,'/cart');
});
}
else if(cart)
{
Ticket.findOne({_id:req.params.id},function (err, ticket)
{
if (err)
{
console.log("Error finding ticket "+err);
req.session.flash =
{
type: 'danger',
intro: 'Ooops!',
message: 'There was an error finding that ticket.'
};
return res.redirect(303, '/cart');
}
else
{
var item =
{
id:ticket._id,
type: ticket.type,
price: ticket.price,
quantity: 1,
time: ticket.time,
date: ticket.date
};
cart.items.push(item);
cart.save();
return res.redirect(303,'/cart');
}
});
}
});
};
答案 0 :(得分:0)
解释in this answer尝试将其与Cookie相关联
app.use(express.session({cookie: { path: '/', httpOnly: true, maxAge: null}, secret:'eeuqram'}));
maxAge:null将确保会话在浏览器关闭后到期。