express-semver-routing

1.0.2 • Public • Published

express-semver-routing

An express middleware providing a mechanism for route switching based on a version field of the request. This makes it possible to use semver for express APIs simply by adding a version tag as the first middleware in a route definition.

Usage

const version = require('express-semver-routing')(/* optional arguments /*)

app.get('/api/resource', version('1.x.x'), ...)
app.get('/api/resource', version('2 - 3.1.x'), ...)

Options

Requiring the middleware returns a function that takes optional arguments.

Defaults:
{
  errorConstructor: Error, // (Optional) Constructor used to create errors for invalid versions
  header: 'accept-version' // (Optional) Which header to check for the request version
  getVersion: undefined,   // (Optional) Function which receives the express req as a parameter for custom
                           //            version fields allowing more flexibility than the `header` option
}

Note: getVersion and header cannot be used together (will throw if you do) as it'd be ambiguous where to get the version

Additional Examples

// Using a field from the querystring
const version = require('express-semver-routing')({
  getVersion: (req) => req.query && req.query['api-version']
})
// Using a default version
const version = require('express-semver-routing')({
  getVersion: (req) => req.get('accept-version') || '1.0.0'
})

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.2
    2,497
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.2
    2,497
  • 1.0.1
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i express-semver-routing

Weekly Downloads

2,366

Version

1.0.2

License

MIT

Last publish

Collaborators

  • nmjohnson