title: node.js middleware and js encapsulation
body: <p>I'm new to javascript, and jumped right into node.js. I've read a lot of theory, and began well with the practical side (I'm writing an API for a mobile app), but I have one basic problem, which has lead me to middleware. I've successfully implemented a middleware function, but I would like to know if the use I'm giving the idea of middleware is OK, and also resolve the original problem which brought me to middleware. My question is two-fold, it's as follows:</p>
  
  <p>1) From what I could gather, the idea of using middleware is repeating a process before actually processing the request. I've used it for token verification, as follows:
  Only one of my urls doesn't receive a token parameter, so</p>
  
  <p>app.js</p>
  
  <pre><code>app.get('/settings', auth.validateToken, auth.settings);
  </code></pre>
  
  <p>auth.js</p>
  
  <pre><code>function validateToken(req, res, next){ //code };
  </code></pre>
  
  <p>In validateToken, my code checks the token, then calls next() if everything is OK, or modifies res as json to return a specific error code.<br>
  My questions regarding this are: a) Is this a correct use of middleware? b) is there a [correct] way of passing a value onto the next function? Instead of calling next only if everything is OK, is there a [correct] way of calling next either way, and knowing from inside the next function (whichever it is), if the middleware was succesful or not? If there is, would this be a proper use of middleware? This precise point brings me to my original problem, and part two of this question, which is encapsulating functions:</p>
  
  <p>THIS PART WAS FIXED, SEE <a href="https://stackoverflow.com/questions/15420505/node-js-middleware-and-js-encapsulation#comment21814036_15420505">MY SECOND COMMENT</a>.<br>
  2) I discovered middleware trying to simply encapsulate validateToken, and be able to call it from inside the functions that the get handlers point to, for example auth.settings.
  I'm used to common, sequential programming, and not in javascript, and haven't for the life of me been able to understand how to do this, taking into account the event-based nature of node.js.<br>
  What I want to do right now is write a function which simply verifies the user and password. I have it perfectly written inside a particular handler, but was about to copy-paste it to another one, so I stopped. I want to do things the right way from scratch, and understand node.js. One of the specific problems I've been having, is that the error code I have to return when user and password don't match are different depending on the parent function, so I would need this function to be able to tell the callback function "hey, the password and user don't match", so from the parent function I can respond with the correct message.<br>
  I <em>think</em> what I actually want is to write an asynchronous function I can call from inside another one.</p>
  
  <p>I hope I've been clear, I've been trying to solve this on my own, but I can't quite finish wrapping my head around what my actual problem is, I'm guessing it's due to my recent introduction to node.js <em>and</em> JS.
  Thanks in advance! Jennifer.</p>