Home

Awesome

koa-ts

The best practice of building Koa2 with TypeScript. 中文


Usage

  1. Run npm init koa-ts

  2. Install dependencies: yarn

  3. Rename .env.example to .env, and run prisma db push to synchronize the data model

  4. Start the server: yarn dev. visit: http://127.0.0.1:3000/apis/sessions

(Optional) the project has built-in a docker compose, run yarn dev:db to run database automatic.


Project Layout

├── app
│   ├── controllers         ---  server controllers
│   ├── helpers             ---  helper func (interceptor / error handler / validator...)
│   ├── jobs                ---  task (periodic task / trigger task / email server...)
│   ├── entities            ---  database entities/models
│   └── services            ---  adhesive controller and model
├── config
│   ├── constants        ---  environment variable
│   ├── koa.middlewares     ---  middlewares for Koa
│   ├── routing.middlewares ---  middlewares for Routing Controller
│   ├── routing.options     ---  configs for Routing Controller
│   ├── bootstrap           ---  lifecycle
│   └── interceptors        ---  global interceptor
│   └── utils               ---  pure functions for help
└── test                    ---  utils for testcase
├── .env           ---  environment file

Feature


Lifecycle

  1. app.ts -> collect env vars constants -> collect env files variables.env

  2. envs ready, call bootstrap.before()

  3. lift routing-controllers -> lift Koa middlewares -> register Container for DI

  4. start Koa & invoke bootstrap.after() after startup


Databases

The project uses Prisma as the intelligent ORM tool by default. Supports PostgreSQL, MySQL and SQLite.


About Environments

When nodejs is running, ENV does not mean NODE_ENV:

For the data settings of each environment, you can refer to the following:


Reference


LICENSE

This project is licensed under the MIT License. See the LICENSE file for more info.