[英]Express.js app works fine locally but doesn't work when deployed on Heroku
[英]Sails JS - Display flash message doesn't work on Heroku(production) but works fine in development
Flash消息仅在开发期间在我的本地计算机上起作用...当我在Heroku上部署该应用程序时它不起作用。 我一直在寻找答案,但是找不到解决我问题的方法。
API /政策/ flash.js
module.exports = function(req, res, next) {
res.locals.flash = {};
if(!req.session.flash) return next();
res.locals.flash = _.clone(req.session.flash);
// clear flash
req.session.flash = {};
next();
};
api / controller / UserController.js(在create操作内)-成功提交表单后,重定向到主页并显示“谢谢”消息。
res.redirect("/");
req.flash('signup-message', '<div class="thankyou-message-wrapper bg-success"><span class="glyphicon glyphicon-remove pull-right" aria-hidden="true"></span><span class="thankyou-message">Thank you for submitting the form!<br> Our administer will review your submission and publish soon.</span></div>');
view / main / index.ejs-这是我从UserController.js呈现消息的方式
<%- req.flash('signup-message') %>
是否有人对我在Heroku上部署时为何未显示Flash消息有任何见解?
您需要将策略添加到routes.js文件中,以启用主页策略。 例如:
'/': {
view: 'homepage', policy: 'flash'
}
然后在您的UserController中,将您的消息添加到session.flash:
req.session.flash = {
message : '<div class="thankyou-message-wrapper bg-success"><span class="glyphicon glyphicon-remove pull-right" aria-hidden="true"></span><span class="thankyou-message">Thank you for submitting the form!<br> Our administer will review your submission and publish soon.</span></div>'
}
最后,在您的主页中显示消息,首先检查消息是否存在,然后显示:
<% if(flash && flash.message) { %>
<%- flash.message %>
<% } %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.