Skip to content

chicunic/koa-ts-demo

Repository files navigation

koa-ts-demo

Demo of Koa with TypeScript (tsoa & swagger UI)

Usage

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

Router

How to add a new router (e.g. /users):

  1. Create a new file usersController.ts in src/users/ folder. (e.g.src/users/UsersController.ts)
  2. Add a new router in usersController.ts file with @Route('users') decorator.
  3. Add a new class UsersController in usesrsController folder extends Controller class from tsoa package.
  4. Use @Get() decorator to add a new GET method or use @Post() decorator to add a new POST method, etc.

Swagger

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.

About

Demo of Koa with TypeScript (tsoa & swagger UI)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published