Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:03:04+00:00 2026-05-26T21:03:04+00:00

I have been working through the tutorial by Alex Young on using flash messages

  • 0

I have been working through the tutorial by Alex Young on using flash messages. According to this tutorial, one is able to override the default flash messages formatting using dynamicHelpers. Unfortunately there are no details provided on what is going on, and it is not possible to post any comments on the tutorial page to ask relevant questions.

What I fail to see is the relationship between the call to req.flash() in the file ‘app.js’, and the FlashMessage object exported in file ‘helpers.js’. Why would a regular call to req.flash(), which is a standard function in express.js, link to this FlashMessage prototype in the first place? I can’t see how this is happening when I look at the code.

At first I thought the FlashMessage object might have been provided to req.flash() by express.js, in which case we are just extending or overriding it in our helper file. The problem with this is that I cannot find any reference to FlashMessage in the source code of express.js.

I would be really grateful if someone could explain it to me.

The flash message is set in file ‘apps.js’ by calling:

req.flash('info', 'Document created.'); 

The FlashMessage obect is exported ‘helpers.js’:

FlashMessage.prototype = {
  // Get css definition string for icon.
  get icon() {
    switch (this.type) {
      case 'info':
        return 'ui-icon-info';
      case 'error':
        return 'ui-icon-alert';
    }
  },

  // Get css class for message container.
  get stateClass() {
    switch (this.type) {
      case 'info':
        return 'ui-state-highlight';
      case 'error':
        return 'ui-state-error';
    }
  },

  // Returns HTML formatted message.
  toHTML: function() {
    return '<div class="ui-widget flash">' +
           '<div class="' + this.stateClass + ' ui-corner-all">' +
           '<p><span class="ui-icon ' + this.icon + '"></span>' + this.messages.join(', ') + '</p>' +
           '</div>' +
           '</div>';
  }
};

exports.dynamicHelpers = {
  flashMessages: function(req, res) {
    var html = '';
    ['error', 'info'].forEach(function(type) {
      var messages = req.flash(type);
      if (messages.length > 0) {
        html += new FlashMessage(type, messages).toHTML();
      }
    });
    return html;
  }
};

In app.js file the full routing function which calls req.flash is as follows:

// Attach dynamicHelpers to app.
app.dynamicHelpers(require('./helpers.js').dynamicHelpers);

// Routing function which calls req.flash.
app.post('/documents', loadUser, function(req, res) {
  // Create Document object and assign value.
  console.log('Document content: %s', req.body['document']);
  var document = new Document(req.body['document']);
  document.save(function() {
      // Redirect to another page.
      req.flash('info', 'Document created.');
      res.redirect('/documents');
    }
  });
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:03:05+00:00Added an answer on May 26, 2026 at 9:03 pm

    There are 2 different things here:

    a) req.flash() which is implemented by Express itself – not by you, you’re just using that function
    b) your dynamic helper:

    From the Express guide:

    Dynamic view helpers are simply functions which accept req, res, and
    are evaluated against the Server instance before a view is rendered.
    The return value of this function becomes the local variable it is
    associated with.

    app.dynamicHelpers({
      session: function(req, res){
        return req.session;
      }
    });
    

    Let’s “translate” that into your code:

    // Attach dynamicHelpers to app.
    app.dynamicHelpers(require('./helpers.js').dynamicHelpers);
    

    That means that when you call the variable flashMessages in your view code, you’ll get the html representation of those flash variables defined.

    So the most important thing here is to consider you are only using req.flash(), not implementing it. You are implementing a helper that is using that function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going through this tutorial on auto-populating boxes using jQuery and Ajax:
I'm starting out with wxPython and have been working my way through every tutorial
I've been reading through this tutorial for using a SQLite database within an iPhone
I have been working through Microsoft's ASP.NET MVC tutorials, ending up at this page
I am new to asp.net and have been working through the tutorial - up
I have been working through a tutorial ( http://glacialflame.com/category/tutorial/ ) to build an Isometric
I'm working on a symfony project, and I've been through the tutorial but this
I have been working through projects involving packages that do all the web design
I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
Over the years as I have gone through school and been working in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.