我是新手,就照着node.js权威指南书中敲得代码,epress3.x中app.dynamicHelpers已经被取消啦,百度了下说用res.locals方法,可我不知道怎么使用着个方法,页面报res no defined我的代码是这样的 在layout.ejs中如下来取值 <% if (res.locals.success) { %> <div class=“alert alert-success”> <%= success %> </div> <% } %> <% if (res.locals.error) { %> <div class=“alert alert-error”> <%= error %> </div> <% } %> 然后在index.js中以下代码用来传值 newUser.save(function(err) { if (err) { res.locals.error=error; return res.redirect(’/reg’); } req.session.user = newUser; res.locals.success=“注册成功”; res.redirect(’/’); }); }); 我也感觉这样代码有问题,可就是不知道怎么解决,求大神详细解答,我是新手,谢啦…
求大神解答啊???
res.locals.success
is error , success
is true.
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3
什么意思?不会又是利奥的助理吧?
我也在学这本书的例子,刚刚解决了这个问题: 在express3.0中要用req.flash()的话要安装connect-flash,直接npm install connect-flash安装,在app.js中加上: var flash = require(‘connect-flash’); app.use(flash()); app.use(function(req, res, next){ res.locals.user = req.session.user; var err = req.flash(‘error’); if(err.length) res.locals.error = err; else res.locals.error = null; var succ = req.flash(‘success’); if(succ.length) res.locals.success = succ; else res.locals.success = null;
next(); });
layout.ejs不用改,按书上的来就行。
@kingzyl don’t use res.locals.success
, correct answer is use success
see:
<% if (success) { %>
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3
需要一个中间件!+1
@brighthas 这样还是不行啊,我现在是不知道怎么用在express.3.x中不用app.dynamicHelpers 视图助手去实现页面间的传值问题???大神啊
res.locals.user = ...
EJS access
<%=user.xxx%>
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3
@brighthas 好吧,无法理解
我按照你的方法来了,可还是报错 :500 ReferenceError: D:\nodejs\microblog\views\layout.ejs:32<br/> 30| <ul class=“nav”> <br/> 31| <li class=“active”><a href="/">首页</a></li> <br/> >> 32| <% if(!user){ %> <br/> 33| <li><a href="/login">登陆</a></li> <br/> 34| <li><a href="/reg">注册</a></li> <br/> 35| <% }else{ %> <br/><br/>user is not defined 服务器能运行,就是在页面上报错,把user相关除掉就报success not defined 或 error not difined?? 什么情况啊?
@alexdotnet 按照楼上的方法做了,可还是报user not defined ,怎么解决啊?
@kingzyl 你的router 是按照什么写的? 我后来了用了。。 可以!
@alexdotnet这是router文件下的index.js内容
module.exports=function(app){ var crypto = require(‘crypto’) var User = require(’…/models/user.js’) var md5 = crypto.createHash(‘md5’);
app.get(’/’,function(req, res){ res.render(‘index’, { title: ‘MicroBlog’}); });
//注册 app.get(’/reg’,function(req, res){ res.render(‘reg’, { title: ‘Reg’}); });
//提交注册信息
app.post(’/reg’,function(req, res){
if (req.body[‘password-repeat’] != req.body[‘password’]) {
req.flush(“error”,“两次输入密码不一致”);
return res.redirect(’/reg’);
}
//生成口令的散列值
var password = md5.update(req.body.password).digest('base64');
var newUser = new User({
name: req.body.username,
password: password,
});
//检查用户名是否已经存在 User.get(newUser.name, function(err, user) { if (user) err = ‘Username already exists.’; if (err) { req.flush(“error”,err); return res.redirect(’/reg’); }
//如果不存在则新增用户
newUser.save(function(err) {
if (err) {
req.flush("error",err);
return res.redirect('/reg');
}
req.session.user = newUser;
req.flush("success","注册成功");
res.redirect('/');
});
});
});
app.get(’/login’,function(req, res){ res.render(‘login’, { title: ‘Login’}); });
app.post(’/login’,function(req, res){ var password = md5.update(req.body.password).digest(‘base64’);
User.get(req.body.username, function(err, user) { if (!user) { req.flush(“error”,‘用户不存在’); return res.redirect(’/login’); } if (user.password != password) { req.flush(“error”,‘用户口令错误’); return res.redirect(’/login’); } req.session.user = user; req.flush(“success”,‘登入成功’); res.redirect(’/’); }); });
app.get(’/logout’,function(req, res){ req.session.user = null; req.flush(“success”,‘登出成功’); res.redirect(’/’); });
app.get(’/u/:user’,function(req, res){ res.render(‘index’, { title: ‘User’}); });
app.post(’/post’,function(req, res){ res.render(‘post’, { title: ‘Post’}); });
}
layout.ejs内容如下:
<!DOCTYPE html>
<html lang=“zh”>
<head>
<meta charset=“utf-8”>
<title><%=title%></title>
<meta name=“viewport” content=“width=device-width; initial-scale=1.0”>
<link rel='stylesheet' href='/css/bootstrap.css' />
<link rel='stylesheet' href='/css/bootstrap-responsive.css' />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/">Microblog</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="/">首页</a></li>
<% if(!user){ %>
<li><a href="/login">登陆</a></li>
<li><a href="/reg">注册</a></li>
<% }else{ %>
<li><a href="/logout">登出</a></li>
<% } %>
</ul>
</div>
</div>
</div>
</div>
<div id="container" class="container-fluid">
<% if (success) { %>
<div class="alert alert-success">
<%=success %>
</div>
<% } %>
<% if (error) { %>
<div class="alert alert-error">
<%=error %>
</div>
<% } %>
<%- body %>
<hr />
<footer>
<p><a href="http://www.byvoid.com/" target="_blank">BYVoid</a> 2012</p>
</footer>
</div>
<script src="/js/jquery-1.10.2.js"></script>
<script src="/js/bootstrap.js"></script>
<script>
var nav_li= $(".nav-collapse .nav li")
nav_li.click(function(){
nav_li.removeClass();
$(this).addClass("active");
});
</script>
</body>
</html>
app.js是按照楼上给的方法加了个 var flash = require(‘connect-flash’); app.use(flash()); app.use(function(req, res, next){ res.locals.user = req.session.user; var err = req.flash(‘error’); if(err.length) res.locals.error = err; else res.locals.error = null; var succ = req.flash(‘success’); if(succ.length) res.locals.success = succ; else res.locals.success = null;
next(); }); 可还是报错 user not defined ,求指教啊?