npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-dependencies

v1.2.3

Published

Dependency Injection for Express

Downloads

20

Readme

express-dependencies

Dependency Injection for Express

NPM Version Linux Build Test Coverage

Installation

npm install --save express-dependencies

Usage: register and injection components

var express = require('express');
var deps = require('express-dependencies')();

var app = express();

// The first argument is request (req) or resolvers (with or without a request), for example: Transient({ foo, singleton, req }, id) {
function Transient(req, id) {
    this.id = id;
    this.name = 'I am a transient component with id ' + id;
}

// You can use the class declaration instead of the function
class Singleton {
    constructor() {
        this.name = 'I am a singleton component';
    }
}

// Set middleware for injection
app.use(deps.setup(container => {
    container.instance('Foo', { foo: 'bar' });
    container.singleton(Singleton);
    container.transient(Transient);
    container.singletonFactory('SingletonFactory', ({ req, foo }) => {
        const result = {
            ip: req.ip,
            path: req.path,
            foo: foo()
        }
        return result;
    });
    container.transientFactory('TransientFactory', ({ transient }) => {
        const result = {
            transient: transient(0)
        }
        return result;
    });
}));

app.get('/:id', function({ req, foo, singleton, transient, singletonFactory, transientFactory }, res, next) {
    const model = {
        foo: foo(), // Resolve a foo component instance
        singleton: singleton(), // Resolve a singleton component instance
        transient: transient(+req.params.id), // Resolve an instance of transient component with id
        singletonF: singletonFactory(), // Resolve an instance of singleton component created using a factory
        transientF: transientFactory() // Resolve an instance of transient component created using a factory
    };
    res.json(model);
});

The result of request to the local service http://localhost:3000/123

{
   "foo":{
      "foo":"bar"
   },
   "singleton":{
      "name":"I am a singleton component"
   },
   "transient":{
      "id":123,
      "name":"I am a transient component with id 123"
   },
   "singletonF":{
      "ip":"::1",
      "path":"/123",
      "foo":{
         "foo":"bar"
      }
   },
   "transientF":{
      "transient":{
         "id":0,
         "name":"I am a transient component with id 0"
      }
   }
}

Scoped Lifestyle

For every request within a defined scope, a single instance of the component will be returned and that instance will be disposed when the scope ends.

You can declare such dependencies using the transientScoped and transientScopedFactory functions.

app.use(deps.setup(container => {
    container.transientScoped(Transient); // enable scoped lifestyle
}));

In version <= 1.0.4, lifestyle scoped is enabled using the isScoped argument in the transient and transientFactory functions. The argument isScoped is not supported in versions >= 1.0.4.

Resolve instances without injection

If you need to get an instance without injections, you can use the Resolve function. The Resolve function accepts the name of the instance as argument, as well as a possible set of additional arguments to the instance constructor.

var deps = require('express-dependencies')();
const resolver = deps.getResolver('sample');
const sampleInstance = resolver();

or

var deps = require('express-dependencies')();
const sampleInstance = deps.resolve('sample');

The deps can be used as a service locator.

I will be glad to your wishes, suggestions and comments. Please, write to [email protected].