title: Node.js express correct use of bodyParser middleware
body: <p>I am new to node.js and express and have been experimenting with them for a while. Now I am confused with the design of the express framework related to parsing the request body.
  From the official guide of express:</p>
  
  <pre><code>app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(logErrors);
  app.use(clientErrorHandler);
  app.use(errorHandler);
  </code></pre>
  
  <p>After setting up all the middleware, then we add the route that we want to handle:</p>
  
  <pre><code>app.post('/test', function(req, res){ 
    //do something with req.body      
  });
  </code></pre>
  
  <p>The problem with this approach is that all request body will be parsed first before the route validity is checked. It seems very inefficient to parse the body of invalid requests. And even more, if we enable the upload processing:</p>
  
  <pre><code>app.use(express.bodyParser({uploadDir: '/temp_dir'}));
  </code></pre>
  
  <p>Any client can bombard the server by uploading any files (by sending request to ANY route/path!!), all which will be processed and kept in the '/temp_dir'. I can't believe that this default method is being widely promoted! </p>
  
  <p>We can of course use the bodyParser function when defining the route:</p>
  
  <pre><code>app.post('/test1', bodyParser, routeHandler1);
  app.post('/test2', bodyParser, routeHandler2);
  </code></pre>
  
  <p>or even perhaps parse the body in each function that handle the route. However, this is tedious to do.</p>
  
  <p>Is there any better way to use express.bodyParser for all valid (defined) routes only, and to use the file upload handling capability only on selected routes, without having a lot of code repetitions?</p>