在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:diego-d5000/inversify-koa-utils开源软件地址:https://github.com/diego-d5000/inversify-koa-utils开源编程语言:TypeScript 93.3%开源软件介绍:inversify-koa-utilsinversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify) InstallationYou can install
The The BasicsStep 1: Decorate your controllersTo use a class as a "controller" for your koa app, simply add the import * as Koa from 'koa';
import { interfaces, Controller, Get, Post, Delete } from 'inversify-koa-utils';
import { injectable, inject } from 'inversify';
@controller('/foo')
@injectable()
export class FooController implements interfaces.Controller {
constructor( @inject('FooService') private fooService: FooService ) {}
@httpGet('/')
private index(ctx: Router.IRouterContext , next: () => Promise<any>): string {
return this.fooService.get(ctx.query.id);
}
@httpGet('/basickoacascading')
private koacascadingA(ctx: Router.IRouterContext, nextFunc: () => Promise<any>): string {
const start = new Date();
await nextFunc();
const ms = new Date().valueOf() - start.valueOf();
ctx.set("X-Response-Time", `${ms}ms`);
}
@httpGet('/basickoacascading')
private koacascadingB(ctx: Router.IRouterContext , next: () => Promise<any>): string {
ctx.body = "Hello World";
}
@httpGet('/')
private list(@queryParams('start') start: number, @queryParams('count') cound: number): string {
return this.fooService.get(start, count);
}
@httpPost('/')
private async create(@response() res: Koa.Response) {
try {
await this.fooService.create(req.body)
res.body = 201
} catch (err) {
res.status = 400
res.body = { error: err.message }
}
}
@httpDelete('/:id')
private delete(@requestParam("id") id: string, @response() res: Koa.Response): Promise<void> {
return this.fooService.delete(id)
.then(() => res.body = 204)
.catch((err) => {
res.status = 400
res.body = { error: err.message }
})
}
} Step 2: Configure container and serverConfigure the inversify container in your composition root as usual. Then, pass the container to the InversifyKoaServer constructor. This will allow it to register all controllers and their dependencies from your container and attach them to the koa app. Then just call server.build() to prepare your app. In order for the InversifyKoaServer to find your controllers, you must bind them to the import * as bodyParser from 'koa-bodyparser';
import { Container } from 'inversify';
import { interfaces, InversifyKoaServer, TYPE } from 'inversify-koa-utils';
// set up container
let container = new Container();
// note that you *must* bind your controllers to Controller
container.bind<interfaces.Controller>(TYPE.Controller).to(FooController).whenTargetNamed('FooController');
container.bind<FooService>('FooService').to(FooService);
// create server
let server = new InversifyKoaServer(container);
server.setConfig((app) => {
// add body parser
app.use(bodyParser());
});
let app = server.build();
app.listen(3000); InversifyKoaServerA wrapper for an koa Application.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论