Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The best practice of building Koa2 with TypeScript

License

NotificationsYou must be signed in to change notification settings

unix/koa-ts

Repository files navigation

The best practice of building Koa2 with TypeScript.中文


Usage

  1. Runnpm init koa-ts

  2. Install dependencies:yarn

  3. Rename.env.example to.env, and runprisma 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, runyarn 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

  • 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.


Lifecycle

  1. app.ts -> collect env varsconstants -> collect env filesvariables.env

  2. envs ready, callbootstrap.before()

  3. liftrouting-controllers -> lift Koa middlewares -> registerContainer for DI

  4. start Koa & invokebootstrap.after() after startup


Databases

The project uses Prisma as the intelligent ORM tool by default. SupportsPostgreSQL,MySQL andSQLite.

  • You can change the data type and connection method in the.env file
  • After each modification to file/prisma/schema.prisma, you need to runprisma migrate dev to migrate the database.
  • After each modification to file/prisma/schema.prisma, you need to runprisma generate to sync types.

About Environments

When nodejs is running,ENV does not meanNODE_ENV:

  • After NodeJS project is built, we always run it asNODE_ENV=PRODUCTION, which may affect some framework optimizations.
  • NODE_ENV only identifies the NodeJS runtime, independent of the business.
  • You should useENV to identify the environment.

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

  • Development Mode (ENV=development): read configurations fromconfigs/constants/development.ts file, but it will still be overwritten by.env file.

  • Production Mode (ENV=production): read configurations fromconfigs/constants/production.ts file, but it will still be overwritten by.env file.


Reference


LICENSE

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

About

The best practice of building Koa2 with TypeScript

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors5


    [8]ページ先頭

    ©2009-2025 Movatter.jp