New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

node-ninject

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ninject

ninject for nodejs

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
2
-60%
Maintainers
1
Weekly downloads
 
Created
Source

node-ninject

nodejs端的DI库,可以方便的在类的构造函数中注入依赖

class TestController {
  constructor(testService) {
    this.testService = testService; // testService => new TestServie()
  }
  doAction(args) {
    this.testService.doSomething(args);
  }
};

Getting Started

Installation

npm install node-ninject

or

yarn add node-ninject

API

Ninject

init(config: ninjectConfig)

config: ninject的映射配置

createInstance(type: Type, [injects: Object])

type: 类型

injects:注入到实例化链中的上下文对象(注入后当前实例化对象和所有依赖关系的实例化对象都拥有该上下文,在nodejs中传递请求上下文很有用)

NinjectConfig

数组对象,维护类型和别名的映射,ninject从中读取实例化的类型。

{alias: 'testService', bind: TestService}

Usage

ninject-config.js

const TestService = require('../services/test-service');
const DepService = require('../services/dep-service');
const Dep2Service = require('../services/dep2-service');

module.exports = [
  {alias: 'testService', bind: TestService},
  {alias: 'depService', bind: DepService},
  {alias: 'dep2Service', bind: Dep2Service},
];

test-controller.js

module.exports = class TestController {
  constructor(testService) {
    this.testService = testService;
  }
  doAction(args) {
    this.testService.doSomething(args);
  }
};

index.js

const ninject = require('node-ninject');
const ninjectConfig = require('./config/ninject-config');
const TestController = require('./controllers/test-controller');

ninject.init(ninjectConfig);

const context = {
  req: {
    url: 'http://xxx.com'
  },
  res: {}
};
try {
  const testController = ninject.createInstance(TestController, {$context: context});

  testController.doAction('I`m is Ninject');
} catch (error) {
  console.log(error)
}

License

node-ninject is licensed under a MIT License.

Keywords

di

FAQs

Package last updated on 06 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts