Skip to content

Commit

Permalink
Add ability to pass in existing express application (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezra-quemuel authored and remojansen committed Mar 20, 2017
1 parent 4da20e0 commit 0626440
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...])`
Expand Down
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 0626440

Please sign in to comment.