title: Can you have express middleware check a route-specific config?
body: <p>I have a node/express project in which for any <em>POST</em> request made, I want to validate the fields in the body of the request.</p>
  
  <p>For this I would like to setup a config per route that handles <em>POST</em> methods, the config would have a list of fields with their constraints, something like this:</p>
  
  <pre><code>router.post('/some_path', function(request, response, next){
      this.fieldsToValidate = [
          {
              name: 'field_name',
              required: true,
              ...
          },
          ...
      ]
      // handle route after validation
  }
  </code></pre>
  
  <p>I would like this to be a per-route configuration, such that I can define a set of fields and their constraints and they will be validated by middleware automatically.</p>
  
  <p>so middleware would look something like:</p>
  
  <pre><code>function(request, response, next){
      if (request.method === 'POST'){
          this.fieldsToValidate.forEach(function(field){
              //do some validation
          });
      }
      next();
  }
  </code></pre>
  
  <p>I understand that middleware is run before the request, but is there any way to have a per-route configuration like this and validate it with middleware?</p>
  
  <p>Thanks in advance.</p>