给团队推广TypeScript,顺手造了个轮子.欢迎品尝O(∩_∩)O
发布于 3 天前 作者 richenlin 602 次浏览 来自 分享

欢迎Star,欢迎Pr。不喜勿喷!

项目地址: https://github.com/thinkkoa/koatty


koatty

Koa2 + Typescript = koatty.

Use Typescript’s decorator implement auto injection just like SpringBoot.

Version npmnpm Downloads

Installation

npm i -g koatty_cli

Quick Start

Check out the quick start example.

Usage

1.Create Project

koatty new projectName

cd ./projectName

yarn install

npm start

2.Create a Controller

koatty controller test

3.Create a Service

koatty service test

3.Create a Middleware

koatty middleware test

4.Create a Model

支持 thinkorm以及 typeorm。其他的ORM自行扩展即可

//thinkorm
koatty middleware test

//typeorm
koatty middleware -o typeorm test

5.Define TestController

import { Controller, BaseController, Autowired, GetMaping, RequestBody, PathVariable, PostMaping, BaseApp, RequestMapping, RequestMethod } from "koatty";
import { TestService } from "../service/TestService";
import { App } from "../App";

@Controller()
export class IndexController extends BaseController {
    app: App;

    @Autowired()
    private testService: TestService;

    init() {
        //...
        this.app.cache = {};
        console.log('IndexController.init()', this.app.cache);
    }

    @RequestMapping("/", RequestMethod.ALL)
    async default(@PathVariable("test") test: string) {
        const info = await this.testService.sayHello();
        return this.ok(test, info);
    }

    @PostMaping("/test")
    test(@RequestBody() body: any) {
        // return this.default('aaa');
        return this.ok("test", body);
    }
}

How to debug

if you use vscode , edit the .vscode/launch.json , like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "TS Program",
            "args": [
                "${workspaceRoot}/src/App.ts" 
            ],
            "runtimeArgs": [
                "--nolazy",
                "-r",
                "ts-node/register"
            ],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}",
            "protocol": "inspector",
            "internalConsoleOptions": "neverOpen"
        }
    ]
}

Select TS Program to debug run. Try to call http://localhost:3000/ .

4 回复

这个很多跟 nest.js 很像了。但是不全。 可以考虑直接上 nest.js 省事

@zuohuadong 1、您所谓的像仅仅是部分注解的使用。整体结构、执行机制、IOC容器完全是自行实现的。 2、再者这个项目是为了在团队推广TS用的,团队之前使用Koa来写。没打算把它做成一个普适性的项目。 3、对于一群习惯了KOA做敏捷开发微服务的人来说,我们要解决的问题是IOC控制反转和依赖注入而已。nest太重、太过繁琐。

最后感谢您的关注

在cn说话就得用中文

回到顶部