title: Node.js REST API versioning the right way?
body: <p>I would like to manage my REST API based on URL version specifying.</p>
  
  <p>For example:</p>
  
  <pre><code>api.mydomain.com/v1/rides/
  // will return all rides based on v1.
  
  api.mydomain.com/v2/rides/
  // will return all rides based on v2 (probably with some braking changes).
  
  api.mydomain.com/rides/
  // will return all rides based on v2, since v2 is the newest.
  </code></pre>
  
  <p>Thats awesome.</p>
  
  <p>Before we get started dealing with the practical way of handling this,
  we should talk about the logical "default newest versioning" - I mean, if user does not going to specify any kind of version, should I serve him with the newest version or throw a 404 not found error?</p>
  
  <p><strong><em>Should I oblige the user for specifying an API version?</em></strong></p>
  
  <p><strong><em>If I do, is there any standard of "parsing" the specific / newest version?</em></strong></p>
  
  <p>I tell you why im concern about this: Lets say that "Dan" have app installed which relays on the newest API endpoint (V1 for example), then I release V2 which has braking changes.
  Since Dans "listens" to the newest version by default, Dans app is going to be crashed.</p>
  
  <p>That is not a good behaviour at all.
  Maybe should I prevent using the "default newest versioning"?
  Maybe should I use Dans app to listen for a specific version, while remote developers accessing my API as a web service can have the privilege to choose between specific version or the newest by default?</p>
  
  <p><strong>Is there any standard?</strong></p>
  
  <p>**</p>
  
  <p><strong>Now lets talk practically.</strong> Lets say that I have a router handling those requests, maybe something like this:</p>
  
  <pre><code>// app.js file
  
  app.use((req, res, next) =&gt; {
    try {
      require('../resources/' + req.url.split('/')[1] + '/' + req.url.split('/')[1] + '-router')(app);
      next();
    } catch(err) {
      dep.cast(res, 404, new Error("Not Found"));
    }
  });
  </code></pre>
  
  <p>And some handler, like this:</p>
  
  <pre><code>// resources/rides/rides-router.js file
  
  module.exports = function(app) {
  
    // GET ride - select a ride
    app.get("/v1/rides/:id", dep.verifyToken(), require('./api/v1/get-ride'));
    app.get("/v2/rides/:id", dep.verifyToken(), require('./api/v2/get-ride'));
  
    // POST ride - insert a new ride
    app.post("/v1/rides", dep.verifyToken(), require('./api/v1/set-ride'));
  
  }
  </code></pre>
  
  <p>As you can see, I have handler which sends the requests to the specific divisions in the API, split by V1, V2, etc..</p>
  
  <p>It makes me wonder if its right to have the same page containing the same function over and over in different folders, one for V1 and one for V2.
  Ofcourse, with some braking changes, but they are probably going to be similar. Is not it bordering with repetitive code?</p>
  
  <p>Look at the project structure:</p>
  
  <p><a href="https://i.stack.imgur.com/fUUyr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fUUyr.png" alt="enter image description here"></a></p>
  
  <p>What do you think about this?</p>