关于req.session.user无法赋值、跳转等问题
发布于 8 年前 作者 zhongguozhe 3991 次浏览 来自 问答

新人一枚,在做Express+nodejs博客时 涉及状态栏转换。代码如下:

header.ejs++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<nav> <span><a title=“主页” href="/">HOME</a></span> ** <% if(user){ %>** //===========>第一个问题:无法实现切换状态栏 <span><a title=“登出” href="/logout">LOGOUT</a></span> <span><a title=“发表” href="/post">POST</a></span> <% } else { %> <span><a title=“登录” href="/login">LOGIN</a></span> <span><a title=“注册” href="/reg">REGISTER</a></span> <% } %> </nav> <article> <% if(success){ %> <div><%= success %></div> <% } %> <% if(error){ %> <div><%= error %></div> <% } %>

index.js(路由文件)+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++只贴部分。

router.get(’/’, function (req, res) { res.render(‘index’, { title:‘主页’, ** user: req.session.user, success: req.flash(‘success’).toString(), error: req.flash(‘error’).toString()** }); }); router.get(’/reg’, function (req, res) { res.render(‘reg’, { title: ‘注册’, user: req.session.user, success: req.flash(‘success’).toString(), error: req.flash(‘error’).toString() }); }); router.post(’/reg’, function (req, res) { var name = req.body.name; var password = req.body.password; var email = req.body.email; var password_re = req.body[‘password-repeat’]; if (password != password_re) { req.flash(‘error’, ‘两次输出不一样!!’); return res.redirect(’/reg’); } //生成密码 m5 zhi const md5 = crypto.createHash(‘md5’); var password = md5.update(req.body.password).digest(‘hex’); var newUser = new User({ name: req.body.name, password: password, email: req.body.email }); console.log(newUser); User.get(newUser.name, function (err, user) { if (err) { req.flash(‘error’, ‘打开失败’); returnres.redirect(’/’); } if (user) { req.flash(‘error’, ‘已存在’); return res.redirect(’/reg’); } newUser.save(function (err, user) { if (err) { req.flash(‘error’, err); return res.redirect(’/reg’); } req.session.user = user; console.log(req.session.user); req.flash(‘success’, ‘注册成功!!’); res.redirect(’/’); }); }); }); router.get(’/login’, function (req, res) {//==========>第二个问题:经过添加“状态栏切换功能” //==========>登陆页乱码: //==========>esc is not a function //==========>TypeError: E:\blog\views\login.ejs:1
//==========>>> 1| <%- include header %> //==========> 2| <form method=“post”> //==========> 3| 用户名:<input type=“text” name=“name”/> //==========> 4| placeholder=“请输入用户名”/><br/> res.render(‘login’, { title: ‘登录’ }); }); router.post(’/login’, function (req, res) { var md5=crypto.createHash(‘md5’), password=md5.update(req.body.password).digest(‘hex’); //检查用户是否存在 User.get(req.body.name,function(err,user){ if(!user){ req.flash(‘error’,’!’);
return res.redirect(‘login’); //用户不存在 } if (user.password!=password){ req.flash(‘error’,’!!’); return res.redirect(’/login’); //密码不一样 } //用户信息存入session req.session.user=user;
//=======>(仍旧是第一个问题)这个无法实现赋值,user跑出来为undefined。 req.flash(‘success’,’!’); res.redirect(’/’);// }); }); router.get(’/post’, function (req, res) { res.render(‘post’, { title: ‘发表’ }); }); router.post(’/post’, function (req, res) { }); router.get(’/logout’, function (req, res) { req.session.user=null; req.flash(‘success’,’!!’); res.redirect(’/’); }); module.exports = router;

app.js+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ var index = require(’./routes/index’); app.use(session({ resave:false, saveUninitialized: true, secret: settings.cookieSecret, key: settings.db,//cookie name cookie: { maxAge: 1000 * 60 * 60 * 24 * 30 },//30days store: new MongoStore({ url: ‘mongodb://localhost/db’ }) })); app.use(flash());

粗体为后添加“状态栏切换功能”code

3 回复

问题发出来,好好编辑一下代码,不然2min能解题的变成了10min,好多人都不愿意看了

@DevinXian真不好编辑。。现在如何

自己好了。。。。没做任何修改。。。看了整整一天。。。。奇葩病。。。。 可以跳转,无报错。。。还差一个页面混乱

回到顶部