我目前正在尝试使用connect-flash构建一个应用程序,使用会话(至少在我理解的情况下)将数据从一条路径保存到另一条路径。
有问题的应用程序在本地运行时如预期的那样工作,但是一旦我部署了它,我就得到了一个错误,其中flash对象返回时没有定义。
我已经尽可能地简化了这部分。我的app.js如下所示。
var express = require('express')
, flash = require('connect-flash')
, util = require('util');
var app = express();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('trust proxy', 1);
app.use(express.logger());
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({
key: 'sid',
cookie: { maxAge: 60000 }
}));
// Use connect-flash middleware. This will add a `req.flash()` function to
// all requests, matching the functionality offered in Express 2.x.
app.use(flash());
app.use(app.router);
});
app.get('/', function(req, res){
res.render('index', { message: req.flash('info') }); //here I pass the flash object to the view
});
app.get('/scrape', function(req, res){
req.flash('info', 'Hi there!')
res.redirect('/');
});
app.get('/no-flash', function(req, res){
res.redirect('/');
});
app.listen(process.env.PORT);我唯一的视图包含以下代码。
<!DOCTYPE html>
<html>
<head>
<title>Express 3.x Example for connect-flash</title>
</head>
<body>
<p>
<a href="/scrape">Scrape</a> |
<a href="/no-flash">No Flash</a>
</p>
<% if (message) { %>
<p><%= message %></p>
<% } %>
</body>
</html>此代码取自connect-flash文档中给出的示例
我有一种感觉,可能有一些额外的配置我需要做的,任何帮助非常感谢。
编辑:奇怪的是,req.flash似乎是持久的,但它的内容却并非如此。
重定向前的req.flash console.logged给出:
: [Function: _flash]在重定向之后,它会生成:
: [Function: _flash]然而,我将其设置为的info flash对象具有以下行为。
之前:
[Hi there!]在此之后:
: []发布于 2019-11-20 03:19:37
调试它第一步是在本地主机和生产环境中执行console.log( req ),并查看这两个环境是否都为请求附加了flash属性。
https://stackoverflow.com/questions/58941065
复制相似问题