先贴下代码 node.js 6.0.0 express 4.13.1 package.json { “name”: “microblog”, “version”: “0.0.0”, “private”: true, “scripts”: { “start”: “node ./bin/www” }, “dependencies”: { “body-parser”: “~1.13.2”, “cookie-parser”: “~1.3.5”, “debug”: “~2.2.0”, “ejs”: “~2.3.3”, “express”: “~4.13.1”, “morgan”: “~1.6.1”, “serve-favicon”: “~2.3.0”, “mongodb”: “1.4.15”, “express-session”: “1.9.1”, “connect-mongo”: “0.4.1”, “connect-flash”: “0.1.1”, “mongoose”:“4.1.10” } } app.js 代码如下 : var express = require(‘express’);//生成一个express实例 app。 var path = require(‘path’); var favicon = require(‘serve-favicon’); var logger = require(‘morgan’); var bodyParser = require(‘body-parser’);
var session = require(‘express-session’);//new var cookieParser = require(‘cookie-parser’); var MongoStore = require(‘connect-mongo’)(session);//new
var routes = require(’./routes/index’); var settings = require(’./settings’);//new var app = express();
app.set(‘port’, process.env.PORT || 3000); app.set(‘views’, path.join(__dirname, ‘views’)); app.set(‘view engine’, ‘ejs’);
app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, ‘public’)));
routes(app);
app.use(function(req, res, next) { var err = new Error(‘Not Found’); err.status = 404; next(err); });
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 }); }); }
app.use(function(err, req, res, next) { res.status(err.status || 500); res.render(‘error’, { message: err.message, error: {} }); });
app.use(session({ secret: settings.cookieSecret, key: settings.db,//cookie name cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days store: new MongoStore({ db: settings.db, //host: settings.host, host: “localhost”, port: settings.port, }), resave:false, saveUninitialized:true })); module.exports = app;
index.js 文件代码如下 var crypto = require(‘crypto’), User = require(’…/models/user.js’); module.exports = function(app) { app.get(’/’, function (req, res) { res.render(‘index’, { title: ‘主页’ }); }); app.get(’/reg’, function (req, res) { res.render(‘reg’, { title: ‘注册’ }); }); app.post(’/reg’, function (req, res) { var name = req.body.name, password = req.body.password, password_re = req.body[‘password-repeat’]; //检验用户两次输入的密码是否一致 if (password_re != password) { req.session(‘error’, ‘两次输入的密码不一致!’); return res.redirect(’/reg’);//返回注册页 } //生成密码的 md5 值 var md5 = crypto.createHash(‘md5’), password = md5.update(req.body.password).digest(‘hex’); var newUser = new User({ name: name, password: password, email: req.body.email }); //检查用户名是否已经存在 User.get(newUser.name, function (err, user) { if (err) { req.session(‘error’, err); return res.redirect(’/’); } if (user) { console.log(“user has been existed”); req.session(‘error’, ‘用户已存在!’); return res.redirect(’/reg’);//返回注册页 } //如果不存在则新增用户 newUser.save(function (err, user) { if (err) { req.session(‘error’, err); return res.redirect(’/reg’);//注册失败返回主册页 } req.session.user = newUser;//用户信息存入 session req.session(‘success’, ‘注册成功!’); res.redirect(’/’);//注册成功后返回主页 }); }); }); 希望高手能给与解答。
req.session(‘success’, ‘注册成功!’);
应该是这样子写吧
req.session.success = ‘注册成功!’;
我觉得你应该是MongoStore配置错了,你仔细检查下配置看看。
配置没有错,报错信息说的很清楚:req.session不是函数,所以不能req.session(‘success’, ‘注册成功!’)这么用。一楼说的对。如果配置错了,会报req.session undefiend!
@jiangli373 恩,改完之后,又报错了 注册时两次输入密码不同时,提交,报错提示:Cannot set property ‘error’ of undefined index.js:18 index.js文件 第18行信息
@zouzhenxing 恩 好的我仔细检查下
@haoyong 改好req.session.success=“注册成功!”; req.session.error=err; 这样的形式之后,依然有报错,哭了要。 Cannot set property ‘error’ of undefined at /home/dupd/microblog/routes/index.js:18:22 怎么回事呀
@jiangli373 恩恩 问题完美解决,非常感谢!