1.koa-ts-cli

The company’s NodeJS KOA backend project was recently refactored with TS.

There are a number of configuration issues that can be encountered when building an entire project, such as integrating koA, typescript, JEST, and ESLint.

Considering the reuse of the project, I wrote a KOA-TS-CLI scaffold and provided three project templates: Basic template (KOA-TS-base-template), full template (KoA-ts-full-template), and full stack template (KoA-TS-full-stack-template).

npm i -g koa-ts-cli
koats create myProject
 # Choose your favorite template.
 
 cd myProject
 npm install
Copy the code

Github address: github.com/SimpleCodeC…

NPM package address: www.npmjs.com/package/koa…

Note: Different templates may work slightly differently, see the documentation for each template

2. Selection of Templates

Koats provides three TEMPLATES based on TS.

  • koa-ts-base-template

    This is a clean koA + TS + Jest + ESLint + Apidoc + Docker engineering template that implements the most basic KOA middleware and configuration etc. If you only need a basic KOA + TS engineering configuration, this template is for you.

    Template address: github.com/SimpleCodeC…

  • koa-ts-full-template

    This is a relatively complete KOA + TS engineering structure, on the basis of koA-TS-BASE-template, added the following functions:

    1. Integrated mysql database
    2. Support for different development environment configurations: Development, production, testing
    3. A unified interface response specification is defined
    4. Middleware that defines unified interface error handling

    Template address: github.com/SimpleCodeC…

  • koa-ts-full-stack-template

    This is a full-stack KOA + TS engineering structure, based on the KOA-TS-full-template, with some modifications to the engineering configuration to enable multi-engineering compilation.

    This project consists of three ends:

    • Client: Used to store front-end code
    • Server: For back-end code
    • Common: Used to put code common to both the front and back ends, such as parameter types for each interface and response types for each interface

    Template address: github.com/SimpleCodeC…

3. Common features

Common features of these three templates:

✓ koa v2

✓ Typescript

✓ Jest

✓ APIDOC

✓ Docker

✓ Eslint

✓ Husky

4. Install KOA-TS-CLI

 npm i -g koa-ts-cli
Copy the code

5. Create projects

 koats create myProject
 # Choose your favorite template.
 
 cd myProject
 npm install
Copy the code

6. Development mode

 cd myProject
 npm run dev
Copy the code

Note: Different templates may work slightly differently, see the documentation for each template

7. Release mode

 npm run build
 cd myProject/dist
 npm run prod
Copy the code

Note: Different templates may work slightly differently, see the documentation for each template

8. Run under Docker

 cd myProject
 npm install
 npm run build
 sudo docker build -t koa-ts-api-server .
 sudo docker run -it --name koa-ts-api-server -p 8080:8080 koa-ts-api-server
Copy the code

9. Run test cases

 cd myProject
 npm run test
Copy the code

10 and thank

If you have any problems, please come and make an issue!

If this scaffold is helpful for your work, study or climbing pit, please move your hands and give a Star, Thanks!

Github address: github.com/SimpleCodeC…

NPM package address: www.npmjs.com/package/koa…