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

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

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


koatty

Koa2 + Typescript = koatty.

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

Version npm npm 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/ .

3 回复

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

在cn说话就得用中文

回到顶部

AltStyle によって変換されたページ (->オリジナル) /