Demo of Koa with TypeScript (tsoa & swagger UI)
Start with development mode:
pnpm run start:devStart with production mode:
pnpm run start:prodFormat code:
pnpm run formatLint code:
pnpm run lintBuild code:
pnpm run buildHow to add a new router (e.g. /users):
- Create a new file
usersController.tsinsrc/users/folder. (e.g.src/users/UsersController.ts) - Add a new router in
usersController.tsfile with@Route('users')decorator. - Add a new class
UsersControllerinusesrsControllerfolder extendsControllerclass fromtsoapackage. - Use
@Get()decorator to add a new GET method or use@Post()decorator to add a new POST method, etc.
The swagger.json file is generated by tsoa package. The swagger UI is served by koa2-swagger-ui package.
How to generate swagger.json:
pnpm run build:swaggerThis command will generate swagger.json file in dist/static folder and routes.ts file in src/routes folder. Please and these two files to .gitignore file and DO NOT commit them to git.
The development mode will generate swagger.json and routes.ts automatically so you don't need to run this command.
The swagger.json file is used by swagger UI to display the swagger UI page. The routes.ts file is used by @koa/router in Koa.
If you want to show the swagger UI page, please visit http://localhost:8080/api/v1 in your browser.