简体   繁体   English

req.flash()节点表示4

[英]req.flash() node express 4

I like use req.flash(), but didnt work. 我喜欢使用req.flash(),但是没有用。 I tried different ways. 我尝试了不同的方法。 Result was nothing. 结果什么都没有。 #{message} is empty. #{message}为空。 I dont know why... thanks to the help! 我不知道为什么...感谢帮助!

server.js: server.js:

var express = require('express');
var flash = require('connect-flash');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session')

var app = express();

app.use(cookieParser('secret'));
app.use(session({cookie: { maxAge: 60000 }}));
app.use(flash());

controller: 控制器:

app.get('/feedback', function (req, res) {
    res.render('feedback', { message: req.flash('info')
    });
});

app.post('/fb', function(req, res) {
    req.flash('info', 'hello world');
    res.redirect('/feedback');
});

view.jade: view.jade:

           p #{message}

Render your view again with the flash as your message object. 以Flash作为消息对象再次渲染视图。

app.post('/fb', function(req, res) {
   res.render('feedback', { 
      message: req.flash('hello world')
   });
});

Displaying error message we can use connect-flash node module 显示错误消息,我们可以使用connect-flash节点模块

configuring flash module 配置闪存模块

var flash = require('connect-flash'); var flash = require('connect-flash');

app.use(flash()); app.use(flash()); connect-flash module expose the req.flash() method that allow you create and retrieve the flash message connect-flash模块公开req.flash()方法,使您可以创建和检索Flash消息

example

app.get('/flash', function(req, res){
  // Set a flash message by passing the key, followed by the value, to req.flash(). 
  req.flash('info', 'Flash is back!')
  res.redirect('/');
});

app.get('/', function(req, res){
  // Get an array of flash messages by passing the key to req.flash() 
  res.render('index', { messages: req.flash('info') });
});

follow this link https://www.npmjs.com/package/connect-flash 跟随此链接https://www.npmjs.com/package/connect-flash

inside jade you can access 里面的玉你可以进入

local.messages 本地消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM