node-dependency-injection-express-middleware

Node Dependency Injection Express Middleware

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
node-dependency-injection-express-middleware
1.2.24 years ago6 years agoMinified + gzip package size for node-dependency-injection-express-middleware in KB

Readme

Node Dependency Injection Express Middleware
NDI Logo
A Node Dependency Injection middleware for Express
Npm Version Build Status Dependencies DevDependencies Code Coverage Maintainability Coding Standard Known Vulnerabilities Npm Downloads License

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

Resources