自己撸的koa-ioc 插件

Posted by Panda on 2019-12-30

koa-ioc

ioc middleware for koa

  • 指定一个服务目录,在启动服务app时,自动灌入服务路由
  • 基于koa-router二次封装 app.get,app.post,app.put 等均可使用。

image

installation

unsing npm :

1
npm install kao-ioc

API Reference

  • ioc_dir( path : string )
    • path : 服务的目录地址

example

Basic usage:

1
2
3
4
5
6
project:
- src
- app.ts
- server(dir)
- testSvr.ts
- ...(other server files)

app.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
import koa from 'koa'
import { resolve } from 'path';
import {ioc_dir} from 'koa-ioc';

let app = new koa();
let ioc = ioc_dir(resolve(__dirname, "./server"));

app.use(ioc);

app.listen(3100, () => {
console.log('server started...');
console.log('http://localhost:3100');
})

testSvr.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Context } from "koa";

async function testPost(ctx: Context) {
console.log('testPost...');
ctx.response.body = "hellow post";
}

async function testGet(ctx: Context) {
console.log('testGet...');
ctx.response.body = 'hello get';
}

module.exports = {
'POST /testSvr/testPort': testPost,
'GET /testSvr/testGet': testGet,
}

Contributing

Please submit all issues and pull requests to the taojiang/koa-ioc repository!

tests

Run tests using ==npm run test==