0.0.10 • Published 6 years ago

mioserv v0.0.10

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

mioserv

Use Dependency Injection (DI) to build the express app.

Motivations

I love inversify and the simplicity of this. But the express-utils of inversify doesn't meet my requirements. And I need somehow to create the express app as fast as possible without overhead.

Installation

$ yarn add mioserv
# or
$ npm install --save mioserv

Step 1: Define services with interfaces

// user.repo.ts
import { Injectable } from "miocore";

@Injectable()
export abstract class UserRepo {
  public abstract findOne(): any;
}

// implement the abstract class/interface above
@Injectable()
export class UserMongoRepo implements UserRepo {
  public findOne() {
    return { username: "q" };
  }
}

Step 2: Create the controller

// user.controller.ts

import { Request, Response } from "express";
import { Controller, Get } from "mioserv";

import { UserRepo } from "./user.repo";

@Controller({
  prefix: "/users"
})
export class UserController {
  constructor(
    private userRepo: UserRepo
  ) {}

  @Get("/")
  public getOne(_: Request, resp: Response) {
    resp.json(this.userRepo.findOne());
  }
}

Step 3: Create the container and express server

// main.ts
import { Container } from "miocore";
import { MioServer } from "mioserv";

import { UserRepo, UserMongoRepo } from "./user.repo";
import { UserController } from "./user.controller";

const container = new Container();
container.register({ target: UserRepo, implementation: UserMongoRepo });

// create server
const server = new MioServer(container);
// register the controller
server.register([
  UserController
]);

// listening
const app = server.build();
app.listen(3000, () => {
  console.log("Server listening...");
});
// navigate to http://localhost:3000/users