Demo of Koa with TypeScript (tsoa & swagger UI)
Start with development mode:
pnpm run start:dev
Start with production mode:
pnpm run start:prod
Format code:
pnpm run format
Lint code:
pnpm run lint
Build code:
pnpm run build
How to add a new router (e.g. /users
):
- Create a new file
usersController.ts
insrc/users/
folder. (e.g.src/users/UsersController.ts
) - Add a new router in
usersController.ts
file with@Route('users')
decorator. - Add a new class
UsersController
inusesrsController
folder extendsController
class fromtsoa
package. - 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:swagger
This 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.