写一个express+mongo,中间用到session存储数据,启动就报错,但是我已经导入了express-session模块 下面是我的app.js var express = require(‘express’); var path = require(‘path’); var MongoStore=require(‘connect-mongo’)(express); var settings=require(‘./settings’); var favicon = require(‘serve-favicon’); var logger = require(‘morgan’); var cookieParser = require(‘cookie-parser’); var bodyParser = require(‘body-parser’); var flash=require(‘connect-flash’); var routes = require(‘./routes/index’); var users = require(‘./routes/users’); var app = express(); var session=require(‘express-session’); // view engine setup app.set('views’, path.join(__dirname, ‘views’)); app.set('view engine’, ‘ejs’);
// uncomment after placing your favicon in /public app.use(favicon(path.join(__dirname, 'public/images’, ‘blog.jpg’)));
app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(flash());
app.use(session({ secret:settings.cookieSecret, key:settings.db,//cookie name cookie:{maxAge:100060602430}, store:new MongoStore({ db:settings.db })
}));
app.use(express.static(path.join(__dirname, ‘public’)));
app.use('/’, routes); app.use('/users’, users);
// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error(‘Not Found’); err.status = 404; next(err); });
// error handlers
// development error handler // will print stacktrace if (app.get(‘env’) === ‘development’) { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error’, { message: err.message, error: err }); }); }
// production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error’, { message: err.message, error: {} }); });
module.exports = app;
= =…你百度的,还是照书上打的把~我分享一下我的代码,我是用mongoose+express-session写的 个人感觉错误在于没有建立session和mongo的链接,导致session存放位置缺失,从而错误 var session = require(‘express-session’); var MongoStore = require(‘connect-mongo’)(session); app.use(session({ secret: 'keyboard cat’, //saveUninitialized: false, // don’t create session until something stored //resave: false, //don’t save session if unmodified store: new MongoStore({ url: 'mongodb://localhost/session’, autoRemove: 'interval’, autoRemoveInterval: 10 // In minutes. Default }), cookie: { maxAge: 900000 } // expire session in 15 min or 900 seconds }));
同是新手,不对地方望指正