我是新手,就照着node.js权威指南书中敲得代码,epress3.x中app.dynamicHelpers已经被取消啦,百度了下说用res.locals方法,可我不知道怎么使用着个方法,页面报res no defined我的代码是这样的 在layout.ejs中如下来取值 <% if (res.locals.success) { %>
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
res.locals.user = ...
EJS access
<%=user.xxx%>
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3
@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 ,求指教啊?