koa-ts
The best practice of building Koa2 with TypeScript. 中文
Usage
required NodeJS > 8.0
-
Run
npm init koa-ts
. -
Install dependencies:
yarn
ornpm i
. -
Lift srever:
yarn start
ornpm start
. visit: http://127.0.0.1:3000/apis/sessions
[Optional] if you need database, set useMongoDB to true.(in configs/customs.ts
).
[Optional] the project has built-in a docker-compose, run npm run mongo
lift mongodb automatic.
Catalog
├── app
│ ├── controllers --- contoller
│ ├── helpers --- helper func (interceptor / error handler / validator...)
│ ├── jobs --- task (periodic task / trigger task / email server...)
│ ├── entities --- database entity (model)
│ └── services --- adhesive controller and model
├── config
│ ├── environments --- environment variable
│ ├── koa.middlewares --- middlewares for Koa
│ ├── routing.middlewares --- middlewares for Routing Controller
│ ├── routing.options --- configs for Routing Controller
│ ├── connection --- database connection
│ ├── bootstrap --- lifecycle
│ ├── customs --- user settings
│ └── interceptors --- global interceptor
│ └── utils --- pure functions for help
└── test
└── apis --- test cases
├── variables.env --- environment file
Feature
-
Separation configuration and business logic.
-
Export scheme model and interface, follow style of TypeScript.
-
Test cases and lint configuration.
-
The best practice for Dependency Injection in Koa project.
-
Deploy by ncc.
-
TypeScript hotload.
Lifecycle
-
app.ts
-> collect env varsenvironments
-> coolect env filesvariables.env
-
envs ready, call
bootstrap.before()
-
configs/connection.ts
connecting external services (e.g. DB / Redis...) -
lift
routing-controllers
-> lift Koa middlewares -> registerContainer
for DI -
start
Koa
。invokebootstrap.after()
after startup -
configs/connection.ts
connected -> invokebootstrap.connected()
About Environments
-
In development (
NODE_ENV=development
), read configurations fromconfigs/environments/development.ts
file, but it will still be overwritten byvariables.env
file. -
In production (
NODE_ENV=production
), read configurations fromconfigs/environments/production.ts
file, but it will still be overwritten byvariables.env
file.
Reference
LICENSE
Licensed under the MIT LICENSE.