Coder Social home page Coder Social logo

express-typescript-ioc's Introduction

NOTICE:

This repository has been moved to https://github.com/inversify/inversify-express-utils in order to make an (official) Inversify npm package out of it. I'll keep this repository alive for now, but please check out the new one for any updates.

express-typescript-ioc

Example Express 4.x project in Typescript using Inversify as an IoC container.

To be used as a starting point for Typescript Express applications.

Usage

Run npm install && typings install followed by npm start will run the application.

Run npm test to run mocha tests.

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 method as a request handler for a particular path and method, where the method is a valid method on the express.Router class.

  • @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 ๐Ÿ˜„).

Example

FooController:

import * as express from 'express';
import { Controller, Get } from '../framework/decorators';
import { FooService } from '../services/foo-service';
import { injectable, inject } from 'inversify';

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

FooService:

import { injectable } from 'inversify';

@injectable()
export class FooService {
    
    private data = {
      1: 'Foo',
      2: 'Bar'  
    };
    
    public get(id: number): string {
        return this.data[id];
    }
}

app.ts (composition root)

/// <reference path="../node_modules/inversify/type_definitions/inversify/inversify.d.ts" />
/// <reference path="../node_modules/reflect-metadata/reflect-metadata.d.ts" />

import "reflect-metadata";
import * as express from 'express';
import { Kernel } from 'inversify';
import { Server } from './framework/server';
import { FooController } from './controllers/foo-controller';
import { FooService } from './services/foo-service';

// set up kernel
var kernel = new Kernel();
kernel.bind<FooService>('FooService').to(FooService);
kernel.bind<FooController>('FooController').to(FooController);

// create server
var server = new Server(kernel);

server
    .build()
    .listen(3000, 'localhost', callback);

function callback() {
    console.log('listening on http://localhost:3000');
}

TODO

express-typescript-ioc's People

Contributors

codyjs avatar vesteraas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.