title: Node.js middleware organization and parameter validation
body: <p>I'm building an express app and I'd like to know how fancy I can get with middleware. Roughly, I want to accomplish the following with middleware.</p>
  
  <p>Done:</p>
  
  <ul>
  <li>Add requestId to all routes</li>
  <li>Authenticate request</li>
  <li>Check whether a user has access to a given resource (apart from
  authentication)</li>
  </ul>
  
  <p>Not done:</p>
  
  <ul>
  <li><strong>A)</strong> Validate parameters for a given route</li>
  <li><strong>B)</strong> Organize middleware in a sane way if it differs from route to route,
  and 3 middlewares are called routinely per route</li>
  </ul>
  
  <p>I have defined my middleware in a separate file, and import it into app.js like so:</p>
  
  <pre><code>var middleware = require('./middleware');
  var requestId = middleware.requestId;
  var authenticate = middleware.authenticate;
  </code></pre>
  
  <p>To apply it to all routes I add it to express config:</p>
  
  <pre><code>var app = express.createServer();
  app.configure(function () {
    app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(requestId); // add requestId to all incoming requests
  });
  </code></pre>
  
  <p>And for route specifics, I add it as an app.get argument:</p>
  
  <pre><code>var routes = require('./v1/routes');
  app.get("/v1/foo", routes.foo);
  app.get("/v1/bar", authenticate, routes.bar);
  </code></pre>
  
  <p><strong>Problem A</strong></p>
  
  <p>I'd love to have middleware that I could use to check parameters</p>
  
  <pre><code>validate('x','y','z')
  </code></pre>
  
  <p>And use it like so for a given route:</p>
  
  <pre><code>app.get("/v1/bar", authenticate, validate('x','y','z'), routes.bar);
  </code></pre>
  
  <p>Is there a good way to do this? Or should I just be validating on per route basis inside the route definition files?</p>
  
  <p><strong>Problem B</strong></p>
  
  <p>Is there a better way to organize and use my middleware that I should consider?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I'm looking for a way to validate parameters that change a lot between routes. The below obviously don't work- I cannot pass params into the middleware- but is there way where I can define middleware that does this and call it as I've said above?</p>
  
  <pre><code>var validateParams = function (req, res, params, callback) {
    // Make sure the required parameters are in the request
    console.log('checking for params '+params);
    for (var i = 0; i &lt; params.length; i++) {
      var param = params[i];
      if(!(param in req.query)){
        logger.info('cannot find param ['+param+'] in req: '+JSON.stringify(req.query));
        res.writeHead(400, {
          "Content-Type": "application/json"
        });
        var out = {
          "err": "request missing required parameters"
        };
        res.end(JSON.stringify(out));
        return;      
      }
    }
    callback();
  }
  </code></pre>