diff --git a/README.md b/README.md index 7b96eee9..83b0295a 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,21 @@ server .listen(3000, 'localhost', callback); ``` +## Using a custom Router +It is possible to pass a custom `Router` instance to `InversifyExpressServer`: + +```ts +let container = new Container(); + +let router = express.Router({ + caseSensitive: false, + mergeParams: false, + strict: false +}); + +let server = new InversifyExpressServer(container, router); +``` + ## Decorators ### `@Controller(path, [middleware, ...])` diff --git a/package.json b/package.json index 5d28158f..6c363986 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "inversify-express-utils", - "version": "3.0.0", + "version": "3.1.0", "description": "Some utilities for the development of express applications with Inversify", "main": "lib/index.js", "jsnext:main": "es/index.js", diff --git a/src/server.ts b/src/server.ts index 2b107138..eabbe0ae 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7,18 +7,24 @@ import { TYPE, METADATA_KEY } from "./constants"; * Wrapper for the express server. */ export class InversifyExpressServer { - private container: inversify.interfaces.Container; - private app: express.Application = express(); - private configFn: interfaces.ConfigFunction; - private errorConfigFn: interfaces.ConfigFunction; + + private _router: express.Router; + private _container: inversify.interfaces.Container; + private _app: express.Application = express(); + private _configFn: interfaces.ConfigFunction; + private _errorConfigFn: interfaces.ConfigFunction; /** * Wrapper for the express server. * * @param container Container loaded with all controllers and their dependencies. */ - constructor(container: inversify.interfaces.Container) { - this.container = container; + constructor( + container: inversify.interfaces.Container, + customRouter?: express.Router + ) { + this._container = container; + this._router = customRouter || express.Router(); } /** @@ -30,7 +36,7 @@ export class InversifyExpressServer { * @param fn Function in which app-level middleware can be registered. */ public setConfig(fn: interfaces.ConfigFunction): InversifyExpressServer { - this.configFn = fn; + this._configFn = fn; return this; } @@ -43,7 +49,7 @@ export class InversifyExpressServer { * @param fn Function in which app-level error handlers can be registered. */ public setErrorConfig(fn: interfaces.ConfigFunction): InversifyExpressServer { - this.errorConfigFn = fn; + this._errorConfigFn = fn; return this; } @@ -52,23 +58,23 @@ export class InversifyExpressServer { */ public build(): express.Application { // register server-level middleware before anything else - if (this.configFn) { - this.configFn.apply(undefined, [this.app]); + if (this._configFn) { + this._configFn.apply(undefined, [this._app]); } this.registerControllers(); // register error handlers after controllers - if (this.errorConfigFn) { - this.errorConfigFn.apply(undefined, [this.app]); + if (this._errorConfigFn) { + this._errorConfigFn.apply(undefined, [this._app]); } - return this.app; + return this._app; } private registerControllers() { - let controllers: interfaces.Controller[] = this.container.getAll(TYPE.Controller); + let controllers: interfaces.Controller[] = this._container.getAll(TYPE.Controller); controllers.forEach((controller: interfaces.Controller) => { @@ -90,14 +96,14 @@ export class InversifyExpressServer { router[metadata.method](metadata.path, ...metadata.middleware, handler); }); - this.app.use(controllerMetadata.path, ...controllerMetadata.middleware, router); + this._app.use(controllerMetadata.path, ...controllerMetadata.middleware, router); } }); } private handlerFactory(controllerName: any, key: string): express.RequestHandler { return (req: express.Request, res: express.Response, next: express.NextFunction) => { - let result: any = this.container.getNamed(TYPE.Controller, controllerName)[key](req, res, next); + let result: any = this._container.getNamed(TYPE.Controller, controllerName)[key](req, res, next); // try to resolve promise if (result && result instanceof Promise) { diff --git a/test/server.test.ts b/test/server.test.ts index c528d76e..31cf1474 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -35,4 +35,23 @@ describe("Unit Test: InversifyExpressServer", () => { expect(configFn.calledBefore(errorConfigFn)).to.be.true; done(); }); + + it("Should allow to pass a custom Router instance as config", () => { + + let container = new Container(); + + let customRouter = express.Router({ + caseSensitive: false, + mergeParams: false, + strict: false + }); + + let serverWithDefaultRouter = new InversifyExpressServer(container); + let serverWithCustomRouter = new InversifyExpressServer(container, customRouter); + + expect((serverWithDefaultRouter as any)._router === customRouter).to.be.false; + expect((serverWithCustomRouter as any)._router === customRouter).to.be.true; + + }); + });