Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inversify-express-utils

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inversify-express-utils

Some utilities for the development of express applications with Inversify

  • 1.0.0-beta.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
52K
decreased by-1.41%
Maintainers
1
Weekly downloads
 
Created
Source

inversify-express-utils

Join the chat at https://gitter.im/inversify/InversifyJS Build Status codecov.io npm version Dependencies img img Known Vulnerabilities

NPM NPM

Some utilities for the development of express applications with Inversify.

Installation

The package is available on npm:

$ npm install --save inversify-express-utils

The type definitions are available in the inversify-dts npm package. Please refer to the inversify docs to learn more about the installation process.

The Basics

Step 1: Decorate your controllers

To use a class as a "controller" for your express app, simply add the @Controller decorator to the class. Similarly, decorate methods of the class to serve as request handlers. The following example will declare a controller that responds to `GET /foo'.

import * as express from 'express';
import { Controller, Get } from 'inversify-express-utils';
import { injectable, inject } from 'inversify';

@Controller('/foo')
@injectable()
export class FooController implements Controller {
    
    constructor( @inject('FooService') private fooService: FooService ) {}
    
    @Get('/')
    private index(req: express.Request): string {
        return this.fooService.get(req.query.id);
    }
}

Step 2: Configure kernel and server

Configure the inversify kernel in your composition root as usual.

Then, pass the kernel to the InversifyExpressServer constructor. This will allow it to register all controllers and their dependencies from your kernel and attach them to the express app. Then just call server.build() to prepare your app.

In order for the InversifyExpressServer to find your controllers, you must bind them to the "Controller" service identifier and tag the binding with the controller's name. The Controller interface exported by inversify-express-utils is empty and solely for convenience, so feel free to implement your own if you want.

import { Kernel } from 'inversify';
import { InversifyExpressServer } from 'inversify-express-utils';

// set up kernel
let kernel = new Kernel();

// note that you *must* bind your controllers to Controller 
kernel.bind<Controller>('Controller').to(FooController).whenTargetNamed('FooController');
kernel.bind<FooService>('FooService').to(FooService);

// create server
let server = new InversifyExpressServer(kernel);

let app = server.build();
app.listen(3000);

InversifyExpressServer

A wrapper for an express Application.

.setConfig(configFn)

Optional - exposes the express application object for convenient loading of server-level middleware.

import * as morgan from 'morgan';
// ...
let server = new InversifyExpressServer(kernel);
server.setConfig((app) => {
    var logger = morgan('combined')
    app.use(logger);
});

.setErrorConfig(errorConfigFn)

Optional - like .setConfig(), except this function is applied after registering all app middleware and controller routes.

let server = new InversifyExpressServer(kernel);
server.setErrorConfig((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

.build()

Attaches all registered controllers and middleware to the express application. Returns the application instance.

// ...
let server = new InversifyExpressServer(kernel);
server
    .setConfig(configFn)
    .setErrorConfig(errorConfigFn)
    .build()
    .listen(3000, 'localhost', callback);

Decorators

@Controller(path, [middleware, ...])

Registers the decorated class as a controller with a root path, and optionally registers any global middleware for this controller.

@Method(method, path, [middleware, ...])

Registers the decorated controller method as a request handler for a particular path and method, where the method name is a valid express routing method.

@SHORTCUT(path, [middleware, ...])

Shortcut decorators which are simply wrappers for @Method. Right now these include @Get, @Post, @Put, @Patch, @Head, @Delete, and @All. For anything more obscure, use @Method (Or make a PR :smile:).

Keywords

FAQs

Package last updated on 22 Jun 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc