13 Actual combat on the server side: initializing the project

This chapter begins with the actual development of the server, including Devops, server and other middleware modules involved.

Since the documentation of NestJS is very complete and a relatively complete booklet of NestJS has been written before, students who are very familiar with NestJS can quickly skip this chapter. In addition, the code of the entire practical chapter will be developed according to the process, and only the key parts will be explained separately. I can write a general framework instead of understanding it in specific code. So if there is any doubt, it means that my design article has not been written well, and the content needs to be supplemented.

Environment construction

In the previous Demo, we have been directly installing the required software services on the server or virtual machine, but this is not conducive to migration and solving various complex environment configuration problems, so this time we will use the method based on Docker Compose to quickly build the required development environment, but this requires you to be familiar with the use of Docker and Docker Compose.

  • Install Docker: docs.docker.com/engine/inst…
  • Install Docker Compose: docs.docker.com/compose/ins…

Check the documentation yourself during the installation process, if you find it troublesome Mac and Windows students can directly download Dockers Desktop

Create a new docker-compose.yml file in any directory, and fill in the following content:

version: '3.3'

volumes:
  mongodb_data:
  mysql_data:

services:
  mongo:
    image: bitnami/mongodb:5.0.8
    volumes:
      - 'mongodb_data:/Users/Shared/mongodb'
    ports:
      - "27017:27017"

  mysql:
    image: bitnami/mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - 'mysql_data:/Users/Shared/mysql'
    ports:
      - "3306:3306"

Run docker-compose up to start the service:

If you do not have a corresponding mirror locally, it will start to pull the corresponding mirror.

If you want to start it in the background, use the docker-compose up -d script, as shown in the figure below after the startup is complete:

If the above problem occurs, it means that the image is not compatible with the M1 chip. The easiest solution is to switch the image of mongodb to mongo:5.0.0-focal.

Next, we can use the client to verify whether the corresponding database is started normally.

Database visualization client

MongoDB

  1. Robo 3T: This is a free MongoDB client that provides an intuitive interface to manage MongoDB databases and supports Windows, MacOS and Linux platforms;
  2. Mongo Management Studio: This is an open source MongoDB client that provides an intuitive interface to manage MongoDB databases. Support for Windows, MacOS and Linux platforms;
  3. Mongo Express: This is a Web based MongoDB client that supports Windows, MacOS, and Linux platforms.

Personally, I am more used to Robo 3T, as for the others, you can do whatever you want, the following is the normal connection to MongoDB

MYSQL

  1. DBeaver: DBeaver is an open source general-purpose database management tool that supports various database systems such as MySQL, PostgreSQL, Oracle, DB2;
  2. HeidiSQL: Provides a graphical interface and supports the Windows operating system;
  3. MySQL Workbench: MySQL Workbench is a free database management tool officially provided by MySQL, providing a graphical interface and supporting Windows, MacOS and Linux platforms

Personally, I am more used to MySQL Workbench, as for others, you can do whatever you want, the following is what it looks like to connect to MYSQL normally:

Correspondingly, other middleware such as Redis can be started in this way. Except for some basic configurations that require special handling, there is no need to pay special attention to environmental issues. Students who are not familiar with Docker can pay attention to the engineering column, which contains some relevant knowledge that can be read together.

NestJS project initialization

Because some students encountered pitfalls during the initialization process before, this time it is a brand new initialization process, using the latest @nestjs/cli version and dependencies for project initialization.

Project initialization

Execute the initialization script:

nest new low-code-test

The project directory structure is as follows:

The above docker-compose.yml file can be moved to the root directory of this project.

Enter the following script to run the project:

pnpm start

Project Split

In the previous project design, we dismantled the entire service into multiple microservices, but creating multiple projects at the same time is very inconvenient in terms of project maintenance, and the shared dependency maintenance between multiple projects is also a headache, so we will use the Monorepo model to develop the entire back-end service.

Execute the following command to create a new sub-application devops, and convert the regular project to Monorepo mode:

nest generate app devops

The new directory structure is as follows, the devops and low-code-test projects have been collected into the apps directory:

At this point, the startup command of the script also becomes as follows:

nest start // Start the default project
nest start devops // start devops submodule

Run the above script directly, of course there will be an error of port duplication, we need to modify the startup port of the devops sub-project:

# file directory `low-code-test\apps\devops\src`
import { NestFactory } from '@nestjs/core';
import { DevopsModule } from './devops.module';

async function bootstrap() {
  const app = await NestFactory. create(DevopsModule);
- await app. listen(3000);
 + await app. listen(3001);
}
bootstrap();

The restart is successful when it appears as shown in the figure below:

Turborepo Management Monorepo

Each sub-application calls each other, and it is very troublesome to open multiple windows to run, so we need to use turbo to help start all sub-applications at once.

Turborepo is a tool for managing Node.js projects based on Monorepo. With Turborepo, you can store multiple Node.js projects in a single Monorepo repository and manage them with a single command. Turborepo provides some commands, such as turborepo add, turborepo build, turborepo test, turborepo lint, etc., to manage the build, test and code quality control of all projects in Monorepo.

  1. Add pnpm-workspace.yaml to the project root directory to convert the project into pnpm workspace mode:
packages:
  - "apps/*"
  1. Install Turborepo with the following command:
pnpm i -w turbo
  1. Add the turbo.json configuration file to the project root directory
{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "dev": {
      "cache": false
    }
  }
}
  1. Add the corresponding package.json to the sub-application, and add the corresponding startup command:
# file directory `low-code-test\apps\devops`
{
  "name": "devops",
  "version": "0.0.1",
  "scripts": {
    "dev": "cd .. & amp; & amp; pnpm start: devops"
  }
}
# file directory `low-code-test\apps\low-code-test`
{
  "name": "low-code-test",
  "version": "0.0.1",
  "scripts": {
    "dev": "cd .. & amp; & amp; pnpm start:lowcode"
  }
}
  1. Modify the startup command of the project root directory:
- "start": "nest start",
 + "start": "turbo run dev",
 + "start:lowcode": "nest start",
 + "start: devops": "nest start devops",
  1. Execute the startup script pnpm start, and it will appear as shown in the following figure:

Create public library

There will also be some common functions repeated between each sub-service. In the Monorepos project, we can use the library capability provided by NestJS to achieve it (the standard mode project can use the npm package to achieve similar functions).

Enter the following command to add the public library:

nest g library comm

Written at the end

So far, the pre-work of the whole project has been completed, and business development will be carried out based on this project in the future. The actual combat process is so boring and unpretentious. If there are students who are unclear about any part, they can leave a message for feedback. We will give feedback for every message.

If you have any questions or better suggestions, welcome to put forward in the comment area.

13 Server combat: initializing the project