Express middleware for resolving controllers with api versioning support.
- Getting started
- Abstract controllers
- Complex file structures
- Defining routes directly in controllers with TypeScript
npm install express-versioning --save
- Create file structure for controllers
- controllers/ - v1/ - users.js - posts.js - comments.js - v2/ - users.js ...
The name of files should have the same name as the resource, which will appear in the path of the route. So that
/users. If you want to use any prefix or suffix for your file names see controllerPattern
// v1/users.js;;// v2/users.js; // link usersV2 to usersV1 via prototype chain// overrideusers.getUser = ;;
- Object literal approach
To make functionality of previous versions available in a newly created version, the newly created version has to be linked to the previous version. This is achieved with
Object.create(v1User). If you don't want to make previous functionality available, don't link the controllers
// v1/users.js// v2/users.js
- Class approach
To make functionality of previous versions available in a newly created version, the newly created version has to extend the controller of the previous version. If you don't want to make previous functionality available, don't extend the controllers of the previous version
If you're using dependency injection, you can set a getter function or an injector (see here)
If you prefer using named exports, make sure, that the filename and the name of the exported controller are the same
- Setup middleware and routes
;app.usecontrollers;app.get'/:version/users',req.controller.getUsersreq, res, next;app.get'/:version/users/:id',req.controller.getUserreq, res, next;
If you want to define abstract controllers for you routes, you can do so by creating an abstract folder on the same level as the version folders:
- controllers/ - abstract/ - users.js - posts.js - comments.js - v1/ - users.js - posts.js - comments.js - v2/ - users.js ...
The name of the abstract folder can be changed (see here).
Complex file structures
If your controllers are structured much more complex like:
- controllers/ - organization-a/ - v1/ - users.js - v2/ - users.js - organization-b/ - v1/ - users.js - sub-organization/ - documents.js ...
express-versioning can also handle this for you and will resolve the controllers of the example to the following routes:
organization-a/:version/users organization-b/:version/users organization-b/:version/sub-organiuation/documents
Defining routes directly in controllers with TypeScript
With TypeScript you're able to define the routes of its corresponding route handlers directly in the controller class of these handlers. Therefore annotations come into play.
To use this feature, you need to install
reflect-metadata and need to set some flags in your
npm install reflect-metadata --save
"experimentalDecorators": true,"emitDecoratorMetadata": true
To define get routes, annotate the appropriate route handlers with a
@Get annotation. The same works for all http
methods, that are supported by express. Please notice, that you should not use the resource name in the path, since
it is already set due to the filename.
resolveRouteHandler need to be set to
Overriding route handlers
When overriding route handlers of previous versions, you must not define the route for its handler again. But must
instead use the
@OverrideRouteHandler annotation. Otherwise
express-versioning throws an error.
This will ensures, that route handlers will not be overridden by accident. Furthermore, it makes clear, that the
@OverrideRouteHandler annotated function is a route handler.
Set resource name explicitly
If you don't want the resource name to be inferred by file name automatically, you can do so, by setting the resource
name explicitly with
or with starting "/"
/*** Path to controllers*/path: string;/*** Regex pattern to recognize a version folder* @default /^(v\d.*)$/*/versionPattern?: RegExp;/*** Regex pattern to recognize controller files* @default /^(.*?)$/*/controllerPattern?: RegExp;/*** Name of directory in which abstract controllers can be* found* @default abstract*/abstractDir?: string;/*** Prints some info to console if true. Default is false* @default false*/debug?: boolean;/*** Indicates if routes handlers should be resolved from* controllers automatically or not* @default false*/resolveRouteHandler?: boolean;/*** Injector to inject controller class instance*/injector?: ;/*** Inject function to inject a controller class instance*/inject?<T>
/*** Stores http method and path as metadata for target prototype*/// ... works for all http methods, that are supported by express
When creating a REST api with version support, there will be some problems you will face:
The definition of the same routes for all versions again and again and again ... and the need to care about which controller should be used for which version:
app.get'/v1/users',usersController.getUsersreq, res, next;app.get'/v2/users',usersController1.getUsersreq, res, next;app.get'/v3/users',usersController2.getUsersreq, res, next;/* ... */
The definition of routes of previous versions for next versions. When creating a next version, not all routes should be created again for a new version. Especially when not all routes have changed compared to the previous version:
// version 1app.get'/v1/users',usersController.getUsersreq, res, next;app.get'/v1/users/:id',usersController.getUserreq, res, next;app.post'/v1/users',usersController.postUserreq, res, next;// version 2app.get'/v2/users',usersController2.getUsersreq, res, next;// no changes here (see "userController" instead of "userController2")app.get'/v2/users/:id',usersController.getUserreq, res, next;app.post'/v2/users',usersController2.postUserreq, res, next;
/v2/users/:id has to be defined again, despite of nothing has changed in version 2 in
But to make this endpoint also available in version 2, we had to do so.
Since DRY is not satisfied, all these issues will probably result in bugs
express-versioning solves these problems for you. So that you don't need to repeat yourself.