node-dependency-injection-express-middleware
Version:
Node Dependency Injection Express Middleware
92 lines (71 loc) • 4.3 kB
Markdown
Node Dependency Injection Express Middleware
============================================

A Node Dependency Injection middleware for Express
[](https://badge.fury.io/js/node-dependency-injection-express-middleware)
[](https://travis-ci.org/zazoomauro/node-dependency-injection-express-middleware)
[](https://david-dm.org/zazoomauro/node-dependency-injection-express-middleware)
[](https://david-dm.org/zazoomauro/node-dependency-injection-express-middleware#info=devDependencies)
[](https://codecov.io/gh/zazoomauro/node-dependency-injection-express-middleware)
[](https://codeclimate.com/github/zazoomauro/node-dependency-injection-express-middleware/maintainability)
[](http://standardjs.com/)
[](https://snyk.io/test/github/zazoomauro/node-dependency-injection-express-middleware)
[](https://www.npmjs.com/package/node-dependency-injection-express-middleware)
[](https://github.com/zazoomauro/node-dependency-injection-express-middleware/blob/master/LICENCE)
Installation
------------
```sh
npm install --save node-dependency-injection-express-middleware
```
Usage
-----------
#### Minimal configuration
```js
import NodeInjectionMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'
const app = express()
const options = {serviceFilePath: 'some/path/to/config.yml'}
app.use(new NodeInjectionMiddleware(options).middleware())
```
### Options
| Name | Required | Type Value | Default Value |
| --------------------------- | -------- | --------------- | -------------- |
| serviceFilePath | true | string | |
| compile | false | boolean | false |
| compilerPass | false | array | [] |
| logger | false | class or object | null |
| containerReferenceAsService | false | boolean | false |
| defaultDir | false | string | null |
### How to use all options
```js
import MyCustomPass from 'CompilerPass/MyCustomPass'
import AnotherAwesomePass from 'CompilerPass/AnotherAwesomePass'
import MyCustomLogger from 'MyCustomLogger'
import NDIMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'
const app = express()
const options = {
serviceFilePath: 'some/path/to/config.yml',
compile: true,
compilerPass: [new MyCustomPass(), new AnotherAwesomePass()],
logger: new MyCustomLogger(),
containerReferenceAsService: true,
defaultDir: '/some/cool/dir',
}
app.use(new NDIMiddleware(options).middleware())
```
### How to get the container
```js
app.get('/some/action', (req, res, next) => {
const container = req.container
const myAwesomeService = container.get('some.awesome.service')
...
})
```
> Please, read the [FULL DOCUMENTATION](https://github.com/zazoomauro/node-dependency-injection/wiki)
Resources
---------
- [Documentation](https://github.com/zazoomauro/node-dependency-injection/wiki)
- [Collaboration and pull requests](CONTRIBUTING.md)
- [Twitter ](https://twitter.com/zazoomauro)
- [Changelog](CHANGELOG.md)