node-dependency-injection-express-middleware
v1.2.2
Published
Node Dependency Injection Express Middleware
Downloads
360
Maintainers
Readme
Node Dependency Injection Express Middleware
A Node Dependency Injection middleware for Express
Installation
npm install --save node-dependency-injection-express-middleware
Usage
Minimal configuration
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
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
app.get('/some/action', (req, res, next) => {
const container = req.container
const myAwesomeService = container.get('some.awesome.service')
...
})
Please, read the FULL DOCUMENTATION