For those of you who are new to NESTJ (Part 1: Project Architecture and Front-end Zero Infrastructure Construction GITLAB)
Here’s the thing
The company sent me to do a provincial project, which was in short supply of staff, so I was responsible for the back end part. Besides, everyone was busy with other tasks, which made the work very hard. In this project, Nest framework was used to write the back end code, which caused inconvenience in communication and a lot of mistakes due to the absence of colleagues. I’m writing this series because I want you to avoid the pitfalls I’ve stumbled on.
Our project requires the operation of GitLab, so here I will also describe in detail how to quickly build a GitLab platform and call its API, as well as the easy operation of Docker, as well as specific examples of database operation that are not mentioned on the TypeOrm website.
Because I am from self-study front turn all of the stack, I understand more without the basis of the service side what students want to know, so this series is also good for no backend development experience because the knowledge I will end before the front of the classmates told understand understand, if you want to understand the nest frame that I would take you study.
Note: The Nest website is really good, but this post is more practical.
The first thing to do
Since there are a lot of relevant contents, we will only do the most basic functions in the first part, as well as the construction of GITLAB. We will talk about various operations in detail later.
I. Initializenest
Nest is pretty much the same as Koa and so on, so I’m not going to make any big ideas, so let’s just get started, and then we’ll summarize what Nest is all about.
Node and NPM I won’t talk much about the front-end prerequisites
Step 1: Install the CLI tools globally and build the project.
$NPM i-g @NestJS/CLI $nest new project name
I started a project called share. Let’s run it in development mode. The next chapter describes running it in debug mode.
CD share yarn start:dev // This way every time you make a change to your code, it will take effect immediately
By default, port 3000 is accessible:
Shortcut commands create everything
Nest has a lot of smart and easy to use commands for building modules. I’ve listed them here and you can check them out later.
Since it is recommended to put each module in SRC /modules, the full path is written below.
The command | The effect |
---|---|
Nest G Controller modules/ name | Setting up a controller |
Nest G service modules/ name | Create a server |
Nest G module modules/ name | Create a module |
Nest g guard | Create a guard |
Nest g interceptor | Create an interceptor |
III. Controller
Let’s start by creating a Users controller that accepts the route associated with ‘XXXX /users’.
nest g controller modules/users
We’ll leave the test file alone, but this xxxxx.controller.ts file will be used to handle routing and some decorator handling of parameters.
import { Controller, Get } from '@nestjs/common'; @controller ('users') export class UsersController {@get () getUsersList() {return 'Get user list '; }}
@Get()
This decorator is the ReceiveA get request
It means that it is from@nestjs/common
To obtain.getUsersList
The name of the function simply describes the purpose of the function.@Controller('users')
Specifies a match for a route.
Let’s see the effect:
Four.service
Perform specific operations
Of course, we can’t use the Controller to perform specific operations. We need the companion service to perform the specific operations. We can still create it from the command line.
nest g service modules/users
import { Injectable } from '@nestjs/common'; @Injectable() export class UsersService {getUsersList(){return [{name:' Injectable ',}, {name:' Injectable '}]}}
letcontroller
callservice
Share/SRC/modules/users/users. The controller. The ts is amended as follows:
import { Controller, Get } from '@nestjs/common'; import { UsersService } from './users.service'; @Controller('users') export class UsersController { constructor( private readonly usersService: UsersService ) { } @Get() getUsersList() { return this.usersService.getUsersList(); }}
Note: UsersService is declared in the constructor argument, and the method inside is called with this.usersService.
Five.nest
The default processing return value type ispromise
In the case
nest
The framework will automatically return us to the userpromise
Convert to a concrete result, so the user will receive the same return value in the following three ways.
Six. You need a server
Of course you have a choiceThe virtual machine
, I directly bought a one-month server from Tencent Cloud. I want to emphasize one point here.gitlab
Setup is a configuration requirement,1 the nuclear 1 g
It’s gonna be stalling. I configured it2 nuclear 4 g
It’s still a bit of a lull but it’s working just fine. Mine isCentOS 7.6
.
Seven. Simple use of the front enddocker
It doesn’t matter if you only know the front-end knowledge. Docker is just a tool, you can think it is a plug-in similar to CLI in function. You can upload your written code and configured environment to Docker. We can use it to get a set of NGINX, MySQL, back-end services, front-end services of the complete system, then we practice.
Step 1: Install
$ yum -y install docker-io
Step 2: Launch & view the version
systemctl start docker
$ docker version
After launching, you will see two versions of the client and server (C/S) architecture of the program. The simple understanding is that your operation, that is, the input of the command and the execution of the command are separate, so that you can do the input of the command specified in a particularserver
Execution, don’t worry too much about it.
Step 3: Set the source acceleration
Mkdir -p/etc/docker tee/etc/docker/daemon. Json < < - 'EOF' {" registry - mirrors: "/" https://fwvjnv59.mirror.aliyuncs.com" } EOF
The above command inserts the content into daemon.json.
Eight.docker
downloadgitlab
The mirror
Once the above Docker configuration is complete, we can use it to pull the image of GitLab.
Recommend to look at the official website firstDocker builds the official website of GitLab
We need to change the above code:
Sudo docker run --detach \ --hostname IP address \ --publish 443:443 --publish 13800:80 --publish 13822:22 \ --name \ --restart always \ --volume $GITLAB_HOME/config:/etc/gitlab \ --volume $GITLAB_HOME/logs:/var/log/gitlab \ --volume $GITLAB_HOME/data:/var/opt/gitlab \ gitlab/gitlab-ce:latest
hostname
Change to your own serverip
.name
Name it whatever you want, so we can use it laterdocker
Management.gitlab-ee
togitlab-ce
It can be considered thatce
It’s a community version, enough for us to learn to use.
Look at mirror
docker image ls
See the program in action
docker container ps
I’ll set the name to “gitlab_lulu”, which we’ll use to set up gitlab-runner in the next post.
9. Open the security group (here demonstrates Tencent cloud)
It is not currently accessible13800
Port, need to go to the cloud console to configure.
Because I did not engage in a separate firewall so directly set here in Tencent on the line, if it is installed a firewall machine please execute the following statement.
Firewall-cmd --add-port=13800/ TCP -- permanent firewall-cmd -- reload
Initial password, set Chinese environment
You’ll be able to access yours in two minutesServer IP: 13800
.
The initial account is root. You can set the password as you like.
Pull it down to the bottom
Set Monday to be the first day of the week
Eleven. Registered trumpet, and approved
In actual development, we definitely do not use root account development, we registered a small account.
You will still not be able to log in with this ID, because you will need to use root to register with the following account.
After doing this, you can access it normally.
end.
In the next post we will modify the configuration of GitLab and use GitLab-Runner so that our GitLab can also go through CI/CD flow and officially start calling the GitLab API. Is it not that difficult? Then we will also enter the official development of NEST. This is it. I hope to make progress with you.