使用 TypeScript 构建 Koa2 的最佳实践
The best practice of building Koa2 with TypeScript. 中文
Run npm init koa-ts
Install dependencies: yarn
Rename .env.example to .env, and run prisma db push to synchronize the data model
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:dbto run database automatic.
…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.
Get constraints on your data model with Prisma.
TypeScript hotload.
app.ts -> collect env vars constants -> collect env files variables.env
envs ready, call bootstrap.before()
lift routing-controllers -> lift Koa middlewares -> register Container for DI
start Koa & invoke bootstrap.after() after startup
The project uses Prisma as the intelligent ORM tool by default. Supports PostgreSQL, MySQL and SQLite.
.env file/prisma/schema.prisma, you need to run prisma migrate dev to migrate the database./prisma/schema.prisma, you need to run prisma generate to sync types.When nodejs is running, ENV does not mean NODE_ENV:
NODE_ENV=PRODUCTION, which may affect some framework optimizations.NODE_ENV only identifies the NodeJS runtime, independent of the business.ENV to identify the environment.For the data settings of each environment, you can refer to the following:
Development Mode (ENV=development): read configurations from configs/constants/development.ts file, but it will still be overwritten by .env file.
Production Mode (ENV=production): read configurations from configs/constants/production.ts file, but it will still be overwritten by .env file.
This project is licensed under the MIT License. See the LICENSE file for more info.
暂无开放 Issues,或尚未同步最近议题。