To develop a Node.js application on a local computer, you need to install node.js and prepare a database management system for the application. Installing Node.js locally you can download and use the installation package directly, so that your system contains only one version of Node.js. If you want to have multiple versions of Node.js, you can use the NVM tool to manage multiple versions of Node.js installed on your computer and switch back and forth between them.

Another way to build a Node.js application development environment is to use Docker. Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm Docker swarm I’ll use nest. js as a demo.

demand

Docker can be used locally to install a Docker desktop version, such as Docker for Windows or Docker for Mac. Using Docker Desktop on a Mac shouldn’t be too much of a problem, but running Docker Desktop on Windows will have some requirements.

The system must be Windows 10 Professional or Enterprise, not normal Windows 10, and hyper-V must be enabled on the system.

To prepare

Download and install Docker desktop version, the volume is quite large, it takes a while, you need to use Docker Hub account login before downloading.

  • Windows:https://docs.docker.com/docker-for-windows/install/
  • macOS:https://docs.docker.com/docker-for-mac/install/

Most operations of Docker need to be completed in the command line interface, so you need to prepare a command line interface. MacOS users can use the Terminal of the system, while Windows users are advised to download and install the complete version of Cmder. Then create a Bash as admin command line.

configuration

When Docker creates containers, it needs to use some images. That is, if you don’t have these images on your system, Docker will automatically download the image from a place, save it on your computer, and then create containers based on the image you need.

The default place to download these images is in foreign countries, so sometimes we are slow in China. The solution is to configure Docker to use the domestic image acceleration address. Ali cloud can provide this service, for example, you can use your own ali cloud account login to ali cloud mirror container service, inside you will find a mirror to accelerate address, looks like this: https://wgaccbzr.mirror.aliyuncs.com

Add the acceleration address you found on Aliyun container mirror service to your Docker Registry Mirrors.

Nest. Js development environment

Docker Compose: Docker Compose: Docker Compose

docker-compose.yml

Docker Compose allows you to describe the services your application needs in a file, create a new directory for your project, and then create a docker-comemess.yml file in the root directory. Add two lines of code:

version: '3'

services:Copy the code

After setting up the Docker Compose version with Version, several services required by the development environment will be defined under Services.

.env

A service defined in docker-comemage. yml can use some environment variables and their corresponding values in a separate file called.env, which is a configuration file. Create a blank file called.env under the project root directory.

Prepare the command line tool for the Nest.js application

If you want to develop a Node application based on the Nest.js framework, you can start by installing the command line tool (@nestjs/cli) provided by the framework, which allows you to use commands to create new applications and components needed in the application. However, since we plan to use Docker to build the application development environment, we will not install the tool directly on the computer, because this requires you to install Node.js on the computer.

The docker – compose. Yml file defines a service, its role is to make we can use the Nest, js framework provides the command line tools, open the docker – compose. Yml file, add a command line utility services under the services:

version: '3'

services:
  cli:
    image: nestjs/cli
    volumes:
      - ./app:/workspace
    tty: trueCopy the code

The command line interface (CLI) is a new service that uses the following volumes: nestjs/cli. The command line interface (CLI) is a new service that uses the following volumes: Corresponds to the location /workspace in the container. Tty is set to true to keep the container running.

To open the Terminal of the system, Cmder is recommended for Windows users. Go to the directory where the docker-comemage. yml file is located and run the service:

cd ~/desktop/ninghao-nestjs
docker-compose up -d cliCopy the code

The first line is to enter the directory where docker-comemess. yml file is located. The second line is to run a service called CLI defined in docker-comemess. yml file in the background. Verify that the service is running:

docker-compose ps
        Name           Command   State    Ports  
-------------------------------------------------
ninghao-nestjs_cli_1   /bin/sh   Up      3000/tcpCopy the code

Note that the State of the service is Up, which indicates that the service is running.

docker-compose exec cli /bin/shCopy the code

Your command prompt should look something like this:

/workspace #Copy the code

In this container we can use the command line tool in the Nest.js application to execute:

nestCopy the code

Some help information appears:

Usage: nest [options] [command]

Options:
  -V, --version                                   output the version number
  -h, --help                                      output usage information

Commands:
  new|n [options] [name]                          Generate Nest application
  generate|g [options]  [name] [path]  Generate a Nest element
    Available schematics:
Copy the code

Create the app with the Nest command line tool

Once inside the created CLI container, you can execute the nest command. Let’s use this command to create a Nest.js project. Perform:

nest new appCopy the code

Something like this will appear:

⚡  We will scaffold your app in a few seconds..

CREATE /app/.prettierrc (51 bytes)
CREATE /app/README.md (3370 bytes)
CREATE /app/nest-cli.json (84 bytes)
...Copy the code

Above is a nest new command to create a project, placed under the app directory, although the project is created in the CLI container, but we configured the data volume of the service, so the project file created will also be seen on the local computer. That is, under the directory where the docker-comemage. yml file is located on your local computer, you will see an app directory where you created the Nest.js project.

When developing an application, if you use the Nest command line tool to generate files for your project, you can access the CLI service container and use the Nest command to create what you need.

Creating a project might prompt:

Failed to execute command: git init
Git repository has not been initializedCopy the code

This is because the Nest command initializes a repository after the project is created, but git is not installed in the container, so there will be problems executing the command. You can use Git locally to do source control for your projects.

Note that if you feel that creating a project is slow, you can enter the CLI service and run the following command:

npm config set registry https://registry.npm.taobao.orgCopy the code

Defining application services

In docker-comemage. yml, define another service to run the Nest. Js application:

  nest:
    image: node:${NODE_VERSION}
    working_dir: /home/node/app
    command: npm config set registry https://registry.npm.taobao.org
    command: npm run start:dev
    volumes:
      - ./app:/home/node/app
    ports:
      - ${APP_PORT}: 3000Copy the code

There is a nest service defined above. Since the application we created is based on the Nest.js framework, the name of the service is Nest, but you can change it to whatever name you like.

The nest service uses an image as a node, and the specific version uses an environment variable, NODE_VERSION, which has its corresponding value set in the.env file.

Working_dir goes to the working directory and executes two commands, one to set up the NPM installation source so that it will be faster to install the package later, and the second command to run the development service for the project.

NPM run start:dev, NPM run :dev, NPM run :dev, NPM run :dev, NPM run :dev, NPM run :dev This will run the development service for the Nest.js project.

Ports Sets public ports, that is, set a port on a host (local computer) that corresponds to a port in the container. Once the Nest app development service is running, port 3000 will be used to provide the service. ${APP_PORT} uses an environment variable called APP_PORT, which is specified in the.env file:

11.13 APP_PORT NODE_VERSION = = 3000Copy the code

Note that in the.env file, we set the APP_PORT environment variable to 3000, which means that the public port should be 3000:3000, which means that port 3000 on the local computer corresponds to port 3000 in the service.

With this new Nest service, run it again:

docker-compose up -d nestCopy the code

If all is well, open your browser, go to http://localhost:3000, and you should see a “Hello World”.

Later, when you need NPM install to install some packages for your project, you can access the Nest service and use NPM. To enter the service, perform:

docker-compose exec nest bashCopy the code

Defining data services

Developing your Nest.js app allows you to use a variety of different types of databases. You can create a service for each database system you need. For example, I want to use MySQL in my application, so I can define a MySQL service in docker-comemage. yml:

  mysql:
    image: mysql:${MYSQL_VERSION}
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    ports:
      - ${MYSQL_PORT}:3306
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}Copy the code

The service uses some environment variables. Open.env and define these environment variables and their values in the file:

MYSQL_VERSION=5.7
MYSQL_PORT=3306
MYSQL_DATABASE=nest
MYSQL_USER=nest
MYSQL_ROOT_PASSWORD=root
MYSQL_PASSWORD=passwordCopy the code

The main thing is to set up the version of the MySQL system to use, the port used to access the data service on the local host, and the name, user, and password of the database to be created. You can modify the values of these environment variables in the.env file to change the database configuration.

MySQL > create a nest database. You can use the nest user (password = “nest”) to operate the database. In addition, the password of user root of the database system is set to root.

To define the data service, run the following:

docker-compose up -d mysqlCopy the code

See the log

To view the output logs in the container, run the following command:

docker-compose logs --followCopy the code

The appendix

Docker – compose. Yml:

version: '3'

services:
  cli:
    image: nestjs/cli
    volumes:
      - ./:/workspace
    tty: true
  nest:
    image: node:${NODE_VERSION}
    working_dir: /home/node/app
    command: npm config set registry https://registry.npm.taobao.org
    command: npm run start:dev
    volumes:
      - ./app:/home/node/app
    ports:
      - ${APP_PORT}:3000
  mysql:
    image: mysql:${MYSQL_VERSION}
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    restart: always
    ports:
      - ${MYSQL_PORT}:3306
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
Copy the code

Env:

NODE_VERSION=11.13 APP_PORT=3000 MYSQL_VERSION=5.7 MYSQL_PORT=3306 MYSQL_DATABASE= NEST MYSQL_USER=nest MYSQL_ROOT_PASSWORD=root MYSQL_PASSWORD=passwordCopy the code