Nodejs中关于req.flash的问题。
Nodejs中关于req.flash的问题。
**express3.x已经废弃了req.flash的这个功能,我看官方文档,是“Removed:req.flash() (just use sessions: req.session.messages = [‘foo’] or similar)”是让我们是用req.session来代替是用req.flash么?我遵循开发指南里面的教程,个人感觉req.flash本身只是一个容器而已,只不过用来存储信息而已。所以我在代码中,凡事需要书中用req.flash存储的信息,我全部用req.session来存储了。比如,req.session.error=’some error‘,然后在app.js中使用这样的代码,app.use(function(req,res,next){ res.locals.user=req.session.user; res.locals.success=req.session.success; res.locals.error=req.session.error; })
我以为这样就可以了。但总是提示说模板中的success,user,error无定义。我不能明白这是怎么回事。不是说res.locals里面的变量自动传递给模板引擎吗?
你的这个app.use是app.configure 里面么?
根本不需要配置再app.configure里面啊。根据官方文档,app.configure函数只是一个用来配置根据不同环境而调用不同函数的工具而已,推荐不用。app.configure([env], callback) Conditionally invoke callback when env matches app.get('env'), aka process.env.NODE_ENV. This method remains for legacy reason, and is effectively an if statement as illustrated in the following snippets. These functions are not required in order to use app.set() and other configuration methods. // all environments app.configure(function(){ app.set('title', 'My Application'); }) // development only app.configure('development', function(){ app.set('db uri', 'localhost/dev'); }) // production only app.configure('production', function(){ app.set('db uri', 'n.n.n.n/prod'); }) effectively sugar for: // all environments app.set('title', 'My Application'); // development only if ('development' == app.get('env')) { app.set('db uri', 'localhost/dev'); } // production only if ('production' == app.get('env')) { app.set('db uri', 'n.n.n.n/prod'); }
表示 session
我不熟悉…
GFM 标记的语法看这里, 英文版… 格式调整下吧
http://github.github.com/github-flavored-markdown/
你模板里是 locals.success 么
res.locals.success.
没接触过marddown,上文的格式确实很挫,下次会注意修改的。
我看别人的代码里面再模板里面都是直接调用变量的。## View options ##
The "view options" setting is no longer necessary, app.locals are the local variables merged with res.render()'s, so app.locals.pretty = true is the same as passing res.render(view, { pretty: true }).
这是express的官方说明啊。
在Express 3.x中,req.flash()
确实已经被废弃了。开发者建议直接使用req.session
来存储消息或其他临时数据。
根据你的描述,你已经尝试将错误信息等通过req.session
来存储,但在模板中仍然无法访问到这些变量。这可能是因为你没有正确地设置中间件来处理res.locals
。
以下是一个完整的示例,展示了如何正确配置并使用req.session
来模拟req.flash
的功能:
示例代码
首先确保你已经在项目中安装了express-session
中间件:
npm install express-session
然后,在你的应用代码中设置session中间件:
const express = require('express');
const session = require('express-session');
const app = express();
// 配置session中间件
app.use(session({
secret: 'your_secret_key',
resave: false,
saveUninitialized: true
}));
// 设置中间件以将session数据传递给视图
app.use((req, res, next) => {
res.locals.user = req.session.user;
res.locals.success = req.session.success || [];
res.locals.error = req.session.error || [];
next();
});
// 示例路由
app.get('/login', (req, res) => {
// 模拟登录失败
req.session.error = ['登录失败,请检查用户名或密码。'];
res.redirect('/');
});
app.get('/', (req, res) => {
res.render('index', { title: 'Home' });
});
// 启动服务器
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
解释
- 配置session中间件:使用
express-session
来管理会话。 - 设置中间件:在每次请求时,将
req.session
中的数据复制到res.locals
中,以便在视图中使用。 - 处理错误:在
/login
路由中模拟一个错误,并将其存储在req.session.error
中。 - 渲染视图:在首页渲染时,可以访问
res.locals.error
中的错误信息。
确保在模板文件(如EJS)中正确引用这些变量,例如:
<% if (error && error.length > 0) { %>
<div class="alert alert-danger">
<% error.forEach(message => { %>
<%= message %><br>
<% }); %>
</div>
<% } %>
以上步骤应该能解决你遇到的问题。如果你仍然遇到问题,请检查是否有其他中间件或配置影响了res.locals
的行为。