totoro-node is a simple to use route management module made specifically to tackle this problem. It generates the routing infrastructure which you can then serve from your service. It also offers extremely simple and easy-to-read ways to deprecate and extend the functionality of specific endpoints or entire API versions. It removes the headache of managing all those versioned routes while making it clear at a glance which endpoints are deprecated and which have been updated.

Inside our index.js file we will added the following API definition using totoro-node:

var app = express() app.use('/api', totoro.rain({

v1: {

endpoints: [

{

route: "/oAuth",

method: "GET",

implementation: routes.authRoutes.oAuth

},

{

route: "/ssoToken",

method: "GET",

implementation: routes.authRoutes.sso

}

]

},

v2: {

endpoints: [

{

route: "/ssoToken",

method: "GET",

implementation: routes.authRoutes.sso

}

]

}

}))

We see a very simple json structure defining the routes for our API and its two versions. The functions in authRoutes.js are of the form:

function oAuth(apiVersion, req, res, next) {

...

}

The apiVersion will contain the API version you defined above, in this example it will either be v1 or v2. This can be used to decide which implementation of the endpoint should be used when serving each request.

The routes added after calling the rain function are as follows:

/api/v1/oAuth

/api/v1/ssoToken

/api/v2/oAuth

/api/v2/ssoToken

All the v1 routes will route to the v1 implementations as we expect but the route for our v2 implementation of the SSO Token endpoint will route to the newer implementation. What happens when we request the v2 implementation of the oAuth Token endpoint? We only have one implementation for it.

totoro-node will carry forward any endpoints you defined in v1 of your API into v2 and any subsequent versions of the API. This is becomes extremely powerful when you use start to specify deprecated endpoints. For example, let’s deprecate our oAuth Token endpoint so we only have SSO Token authentication in our v2 API version.

Simple set deprecated to true in our v1 oAuth endpoint definition:

var app = express() app.use('/api', totoro.rain({

v1: {

endpoints: [

{

route: "/oAuth",

method: "GET",

deprecated: true,

implementation: routes.authRoutes.oAuth

},

{

route: "/ssoToken",

method: "GET",

implementation: routes.authRoutes.sso

}

]

},

v2: {

endpoints: [

{

route: "/ssoToken",

method: "GET",

implementation: routes.authRoutes.sso

}

]

}

}))

Now the routes added after calling the rain function are as follows:

/api/v1/oAuth

/api/v1/ssoToken

/api/v2/ssoToken

We no longer carry forward the oAuth endpoint to our v2 API but it still remains accessible to existing users of the old v1 API.