diff --git a/README.md b/README.md index 6306553b..d765b52c 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,18 @@ let container = new Container(); let server = new InversifyExpressServer(container, null, { rootPath: "/api/v1" }); ``` +## Using a custom express application +It is possible to pass a custom `express.Application` instance to `InversifyExpressServer`: + +```ts +let container = new Container(); + +let app = express(); +//Do stuff with app + +let server = new InversifyExpressServer(container, null, null, app); +``` + ## Decorators ### `@Controller(path, [middleware, ...])` diff --git a/src/server.ts b/src/server.ts index a8e14e51..abcbf892 100644 --- a/src/server.ts +++ b/src/server.ts @@ -10,7 +10,7 @@ export class InversifyExpressServer { private _router: express.Router; private _container: inversify.interfaces.Container; - private _app: express.Application = express(); + private _app: express.Application; private _configFn: interfaces.ConfigFunction; private _errorConfigFn: interfaces.ConfigFunction; private _routingConfig: interfaces.RoutingConfig; @@ -23,13 +23,15 @@ export class InversifyExpressServer { constructor( container: inversify.interfaces.Container, customRouter?: express.Router, - routingConfig?: interfaces.RoutingConfig + routingConfig?: interfaces.RoutingConfig, + customApp?: express.Application ) { this._container = container; this._router = customRouter || express.Router(); this._routingConfig = routingConfig || { rootPath: DEFAULT_ROUTING_ROOT_PATH }; + this._app = customApp || express(); } /** diff --git a/test/server.test.ts b/test/server.test.ts index a9b00a5e..04ac6924 100644 --- a/test/server.test.ts +++ b/test/server.test.ts @@ -72,4 +72,15 @@ describe("Unit Test: InversifyExpressServer", () => { }); + it("Should allow to provide a custom express application", () => { + let container = new Container(); + + let app = express(); + + let serverWithDefaultApp = new InversifyExpressServer(container); + let serverWithCustomApp = new InversifyExpressServer(container, null, null, app); + + expect((serverWithCustomApp as any)._app).to.eq(app); + expect((serverWithDefaultApp as any)._app).to.not.eql((serverWithCustomApp as any)._app); + }); });