Skip to content
The best practice of building Koa2 with TypeScript
TypeScript Shell
Branch: master
Clone or download
Latest commit a0fc0f3 Dec 31, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github build: init github templates Feb 26, 2018
app fix: format entity field style Jun 10, 2019
configs fix: apply middlewares before container use May 15, 2019
test test(apis): upgrade testcase of session Dec 30, 2019
.gitignore Initial commit Feb 26, 2018
LICENSE Initial commit Feb 26, 2018
README.md refactor: remove cli Jun 17, 2019
README_CN.md refactor: remove cli Jun 17, 2019
app.ts feat(lifecycle): add lifecycle May 8, 2019
build.sh build: add build script Apr 22, 2019
compose.yml
nodemon.json feat: add path in tsconfig Apr 22, 2019
package.json 2.0.3 Dec 30, 2019
tsconfig.json feat: remove cors package Apr 22, 2019
tslint.yml chore: update tslint Jun 5, 2019
variables.env fix: fix package links Mar 29, 2019
yarn.lock chore: update tslint Jun 5, 2019

README.md

koa-ts

The best practice of building Koa2 with TypeScript. 中文


Usage

required NodeJS > 8.0

  1. Run npm init koa-ts.

  2. Install dependencies: yarn or npm i.

  3. Lift srever: yarn start or npm 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

  1. app.ts -> collect env vars environments -> coolect env files variables.env

  2. envs ready, call bootstrap.before()

  3. configs/connection.ts connecting external services (e.g. DB / Redis...)

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

  5. start Koa。invoke bootstrap.after() after startup

  6. configs/connection.ts connected -> invoke bootstrap.connected()


About Environments

  • In development (NODE_ENV=development), read configurations from configs/environments/development.ts file, but it will still be overwritten by variables.env file.

  • In production (NODE_ENV=production), read configurations from configs/environments/production.ts file, but it will still be overwritten by variables.env file.


Reference


LICENSE

Licensed under the MIT LICENSE.

You can’t perform that action at this time.