Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Question: Version management when it is at the request header #53

Closed
aidanbon opened this issue Sep 12, 2014 · 4 comments
Closed

Question: Version management when it is at the request header #53

aidanbon opened this issue Sep 12, 2014 · 4 comments

Comments

@aidanbon
Copy link

I like the enrouten directory option. It organize handlers code intuitively. However, when the API version information is not part of the API path (e.g. in the request header). Any systematic treatment for that?

@aredridel
Copy link

Middleware that calls a different router depending on the header, each pointing at a different directory?

Version as a header, though, doesn't play nice with caching the same way that putting it in a URL does.

@sixlettervariables
Copy link

@aidanbon, you could add version-checking middleware which calls next('route') on a mismatch:

// XXX: contrived example
app.use('/api', checkVersion('1.x'), enrouten({ directory: 'api-1.0' }));
app.use('/api', checkVersion('^2.0.0'), enrouten({ directory: 'api-2.0' }));

function checkVersion(routeVersion) {
  // XXX: needs: var semver = require('semver');
  function SemverHeaderCheck(req, res, next) {
    var reqVersion = req.get('X-API-Version');
    if (reqVersion && semver.satisfies(reqVersion, routeVersion)) {
      next();
    }
    else {
      next('route');
    }
  }

  return SemverHeaderCheck;
}

@totherik
Copy link
Member

Haven't seen any activity here in a while, so I assume @sixlettervariables solution was acceptable or the author found an alternate solution. Thanks @sixlettervariables!

@aidanbon
Copy link
Author

Thank you @sixlettervariables for the suggestion, and sorry for the delay to follow up.

Your suggestion looks good. However, since I'm using Kraken meddleware (https://github.com/krakenjs/meddleware) to register my middlewares, I ended up adding a custom middleware right before the enrouten's "router" middleware. The custom middleware simply inspect the request header for version info (say reqVersion), and then rewrite the request url (i.e. req.url = '/v' + reqVersion + req.originalUrl) to let the downstream enrouten to locate the handler at the appropriate directory.

Thanks again for your help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants