Koa2 + Typescript = koatty.
Use Typescript's decorator implement IOC and AOP.
koatty_doc_CN (In progress
[koatty_doc_EN] come soon...
npm i -g koatty_cli
koatty new projectName
cd ./projectName
yarn install
npm start
koatty controller test
koatty service test
koatty middleware test
Supports thinkorm and typeorm. Please expand other ORM by yourself.
//thinkorm
koatty middleware test
//typeorm
koatty middleware -o typeorm test
koatty dto test
import { Controller, BaseController, Autowired, GetMapping, RequestBody, PathVariable, PostMapping, RequestMapping, RequestMethod, Valid } from "koatty";
import { TestDTO } from "../model/dto/TestDTO";
import { TestService } from "../service/TestService";
import { App } from "../App";
@Controller()
export class IndexController extends BaseController {
app: App;
@Autowired()
private testService: TestService;
init() {
this.cache = {};
}
@RequestMapping("/:name", RequestMethod.ALL)
async default(@PathVariable("name") @Valid("IsNotEmpty") name: string) {
const info = await this.testService.sayHello(name).catch((err: any) => this.fail(err.message));
return info;
}
@PostMapping("/test")
@Validated() //need DTOClass
test(@RequestParam() params: TestDTO) {
return this.ok("test", params);
}
}
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/
.
Check out the quick start example.