This npm package provides an ExpressJS middleware to load route controllers based on api versions.
Implementing API Versioning in 15 lines of code:
now just send an API request with the X-Api-Version HTTP header set to a valid version...
Create a map where the key is the version of the supported controller, and the value is a regular ExpressJS route function signature.
const versionRouter = require('express-version-route')
const routesMap = new Map()
routesMap.set('1.0', (req, res, next) => {
return res.status(200).json({'message': 'hello to you version 1.0'})
})
Then, on the route which you wish to version, call the route
function of this module with the map you created:
router.get('/test', versionRouter.route(routesMap))
If no route matches the version requested by a client then the next middleware in the chain will be called. To set a route fallback incase no version matches set a 'default' key on the routes map, for example:
routesMap.set('default', (req, res, next) => {
return res.status(200).json({'message': 'hello to you, this is the default route'})
})
A requested version from the client must be available on the request object at req.version
.
You are encouraged to use this module's twin: express-version-request which is another simple ExpressJS middleware that populates req.version
from the client's X-Api-Version header or from a query string (such as 'api-version=1.0.0')
The key for the routes versions you define can be a non-semver format, for example: 1.0
or just 1
. Under the hood, expression-version-route
uses the semver
module to check if the version found on the request object at req.version
matches the route.
- An API client will send a request to your API endpoint with an HTTP header that specifies the requested version of the API to use:
curl --header "X-Api-Version: 1.0.0" https://www.example.com/api/users
- The
express-version-request
library will parse theX-Api-Version
and sets ExpressJS'sreq.version
property to 1.0.0. - The
express-version-route
library, when implemented like the usage example above will match the 1.0 route version because semver will match 1.0.0 to 1.0, and then reply with the JSON payload{'message': 'hello to you version 1.0'}
.
yarn add express-version-route
yarn test
Project linting:
yarn lint
yarn test:coverage
The project uses the commitizen tool for standardizing changelog style commit messages so you should follow it as so:
git add . # add files to staging
yarn commit # use the wizard for the commit message
An API versioning is a practice that enables services to evolve their APIs with new changes, signatures and the overall API contract without interrupting API consumers and forcing them to repeatedly make changes in order to keep in pace with changes to APIs.
Several methodologies exist to version your API:
- URL: A request specifies the version for the resource:
http://api.domain.com/api/v1/schools/3/students
- Query String: A request specifies the resource in a query string:
http://api.domain.com/api/schools/3/students?api-version=1
- Custom HTTP Header: A request to a resource
http://api.domain.com/api/schools/3/students
with a custom HTTP header set in the requestX-Api-Version: 1
- MIME Type content negotiation: A request to a resource
http://api.domain.com/api/schools/3/students
with anAccept
header that specifies the requested content and its version:Accept: application/vnd.ecma.app-v2+json
There is no strict rule on which methodology to follow and each has their own pros and cons. The RESTful approach is the semantic mime-type content negotiation, but a more pragmatic solution is the URL or custom HTTP header.
Upgrading APIs with some breaking change would lead to breaking existing products, services or even your own frontend web application which is dependent on your API contract. By implementing API versioning you can ensure that changes you make to your underlying API endpoints are not affecting systems that consume them, and using a new version of an API is an opt-in on the consumer. read more...
Several npm projects exist which provide similar API versioning capabilities to ExpressJS projects, and I have even contributed Pull Requests to some of them that provide fixes or extra functionality but unfortunately they all seem to be unmaintained, or buggy.
- https://github.com/Prasanna-sr/express-routes-versioning
- https://github.com/elliotttf/express-versioned-routes
- https://github.com/biowink/express-route-versioning
Liran Tal [email protected]