title: Express.js / Node.js: Order of Routes important and where to put next()?
body: <p>I have my routes and path set up like this:</p>
  
  <pre><code>var path1        = express.Router()
    , path2        = express.Router()
    // .. more modules, e.g. express-jwt ..
  
  module.exports = function(app, passport) {
  
    var path1Func = function(req, res) {
  
    return MODEL.findById(req.params.id, function(err, model) {
  
        if(!model) {
          res.statusCode = 404;
          return res.send({ status: '404 Not found' });
        }
  
        if(!err) {
          return res.send({ status: 'OK', model:model });
        } else {
          res.statusCode = 500;
          return res.send({ status: '500 Server error' });
        }
      });
    };
  
    // .. other functions which essentially look like above path1Func ..
  
    path1.get('/subPath',            path1Func             );
    path2.get('/otherSubPath',            path2Func             );
    path2.post('/entirelyOtherSubPath',           otherPath2Func        );
  
    app.use('/path1', path1);
    app.use('/path2', path2);
  
    app.use('/grid', expressJwt({secret: secret}));  // Protect Paths
  }
  </code></pre>
  
  <p>Now, Depending on ordering of my HTTP requests, POST, GET, etc, they get executed or returning a 404 not found, meaning that express does not resolve requests to some requests, depending of ordering of <code>path1.get</code>, <code>path2.get</code>, <code>path2.post</code>. </p>
  
  <p><img src="https://i.stack.imgur.com/ec4Bo.png" alt="express1Order"><br><br>
  <img src="https://i.stack.imgur.com/Wy5LC.png" alt="express2Order"></p>
  
  <p><em>Above Images depict the 404 I am seeing, depending on the order of the route declarations in my routes.js file. When I reshuffle my order, the 404 is gone and I am served my wanted output</em></p>
  
  <p>This is why I want to employ next() to help me with that - keeping in mind I am using plenty of <code>app.use</code> to complicate matters. I did not manage to do this on my own so far. I am still not clear what next() exactly is and how to use it in above example to make my router work in every route. I did not put my other routes here to save space, but my API is already quite big and will grow even more.</p>
  
  <p>What would be an approach here to make my app serve content, no matter what order my <code>POST</code> and <code>GET</code> requests are ordered?</p>