Parameters passed in Express.js middleware gets cached?

Problem

I've been building my first node.js app using express.js. It has been fun :) I must be having some kind of misconception going, so here goes.

I have some route defined as such:

app.all('/account/summary', apiPOST('AccountSummary', {FromDate: 'default', ToDate: 'default'}), function(req, res){

  var data=req.apiJSON;
  console.log(data);
  res.render('accountsummary', {locals: data, layout: 'layouts/layout'});

});

apiPOST() is defined as such:

apiPOST = function (operation, postParams) {

  return function (req, res, next){

    console.log('RIGHT AT START');
    console.log(postParams);
    console.log('END');

    var currentDate = new Date();
    var day = ('0'+currentDate.getDate()).slice(-2);
    var month = ('0'+(currentDate.getMonth() + 1)).slice(-2);
    var year = ('0'+currentDate.getFullYear()).slice(-4);
    console.log('BEFORE DATES');
    console.log(postParams);
    if (typeof(postParams.FromDate)!='undefined' && (postParams.FromDate=='default' || postParams.FromDate=='')){
      postParams.FromDate=year+'-'+month+'-'+day;
    }

    if (typeof(postParams.ToDate)!='undefined' && (postParams.ToDate=='default' || postParams.ToDate=='')){
      postParams.ToDate=year+'-'+month+'-'+day;
    }

    //automatically add all posted data to postParams
    if (typeof(req.body)!='undefined'){
      for (var key in req.body){
        if (req.body.hasOwnProperty(key)){
          postParams[key]=req.body[key];
        }
      }
    }

    // here is do some talking to an XML web service and convert it to JSON;
    // we use our postParams variable to POST
    next();

  }
}

First off this works fine. When reaching the page on a GET request, it defaults both FromDate and ToDate to today. This page has a form that you may post to specify a new FromData and ToDate. the posted data automatically get added to the postParams and that also works fine.

The problem that I am experiencing is that the next time a user visits the page using GET, the previously POSTed data is still around and so it default to that and not to today.

I cannot figure out why that data is still available. By the looks of it, it is not being posted, but rather remembered in postParams. Is postParams now global?

Thanks!

Problem courtesy of: Jason Prawn

Solution

What's happening is that you're calling apiPOST only once, during the app.all call to configure that route and that one call creates the one postParams parameter object that all future invocations of apiPOST's returned function will share.

Solution courtesy of: JohnnyHK

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.