For you who are using Nestj as a project for the first time (first article: project architecture and front-end zero-based building gitlab)
Things are like this
The company sent me to do a project in other provinces. The project staff is relatively short, so I am responsible for the back-end part, and everyone has other tasks on hand, which makes the work very hard. This project uses the nest
framework to write the back-end code, because my colleagues Not being together causes communication inconvenience or stepping on a lot of pits. I hope that everyone can avoid the pits I stepped on so I wrote this series of articles.
Our project requires the operator gitlab
so here I will detail how to quickly build a gitlab platform and call it a
api
, and docker
ease of operation, as well as typeorm
specific examples cited had no official website of the database.
Since I switched from self-study front-end to full-stack, I better understand what students who have no server-side foundation want to know, so this series of articles is also very suitable for
front-end students who have no experience in back-end development because of related knowledge I will listen to the front-end. Understand the way, if you want to understand the
nest
framework, then I will take you to study.
Note: nest
is really good, but our article is more practical.
The first thing to do
Since there are a lot of related content, we only do the most basic functions in the first article, and gitlab
. We will talk about various operations in detail later.
1. Initialize nest
nest
and koa
and other frameworks are similar, I don't talk about any big concepts, let's start using it directly, and finally we will summarize the characteristics of nest
node and npm, let's not talk about the front-end must
Step 1: Install the cli
tool globally, and generate the project.
$ npm i -g @nestjs/cli
$ nest new 项目名
I have share
a project called 0609a2b77b3e3a here, let us run it in development mode, the next chapter will introduce debugging mode operation.
cd share
yarn start:dev // 这样你每次做出改变代码可以立即生效
The default is 3000 port can be accessed:
2. Quick commands to create everything
nest
has a lot of useful and smart commands, which are convenient for us to create some modules. I will list the commands here first, and you can check them here later.
Since it is recommended to put all modules in src/modules, the complete path is written below.
command | effect |
---|---|
nest g controller modules/name | Build the controller |
nest g service modules/name | Create server |
nest g module modules/name | Create module |
nest g guard first name | Create guard |
nest g interceptor noun | Create interceptor |
3. Controller
We first create a users
controller, which is responsible for receiving the routes related to'xxxx/users'.
nest g controller modules/users
We don't care about the test file. The xxxxxx.controller.ts
file will be used to deal with route is allocated and some decorator processing of the parameters.
import { Controller, Get } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get()
getUsersList() {
return '获取用户列表';
}
}
@Get()
means to receive theget request, which is obtained from
@nestjs/common
.getUsersList
just describes the purpose of this function.@Controller('users')
specifies the matching of the route.
Let's see the effect:
Four. service
perform specific operations
Of course, we can't use Controller
perform specific operations, we need service
this little partner to perform, we can still create it through the command line.
nest g service modules/users
import { Injectable } from '@nestjs/common';
@Injectable()
export class UsersService {
getUsersList(){
return [{
name:'张三',
}, {
name:'李四'
}]
}
}
Let controller
call service
share/src/modules/users/users.controller.ts
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: constructor
declared in the parameters of usersService
, and use this.usersService
call the method inside.
5. nest
handles the case where the return value type is promise
nest
framework will automatically convert the promise
we return to the user into a specific result, so the return value received by the user in the following three ways is the same.
6. Need a server
Of course, you can also choose the virtual machine. I bought a one-month server directly from Tencent Cloud. Here I want to emphasize that
gitlab
has requirements for configuration. 1 core 1G will be very stuck, I configure The
2-core 4G still has a stuttering phenomenon but it can barely be used normally. Mine is
CentOS 7.6
.
7. The front end simply uses docker
It doesn’t matter for students who only know the front-end knowledge, docker
is just a tool, you can think of it as a cli
, everyone uploads the written code and the configured environment to docker
, we can use it We have obtained a nginx
, MySQL
, back-end services, and
front-end services. Next, let's take a real battle.
Step one: install
$ yum -y install docker-io
Step 2: Start & view version
systemctl start docker
$ docker version
After startup, you will see two version information, client and server (C/S) architecture programs, simply understood as your operation, that is, the input command and the execution command are separated, so that you can input The command specifies a specific server
execute, so there is no need to worry too much here.
Step 3: Set source acceleration
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-‘EOF’
{
"registry-mirrors":["https://fwvjnv59.mirror.aliyuncs.com"]
}
EOF
The meaning of the above command is to insert the content into daemon.json
.
8. Use docker
download the gitlab
image
docker
configuration mentioned above, we can use it to pull the mirror image of gitlab
It is recommended to first come to the official website to see docker build gitlab official website
We need to change the above code:
sudo docker run --detach \
--hostname 服务器的ip地址 \
--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
changed to its own serverip
.name
name it casually, so that we can usedocker
manage it in the future.gitlab-ee
changed togitlab-ce
. It can be considered thatce
is the community version, which is enough for us to learn and use.
View mirror
docker image ls
View the running program
docker container ps
I set name
as "gitlab_lulu", we will use this name to set gitlab-runner
next article.
Nine. Open the security group (Tencent Cloud is demonstrated here)
13800
port cannot be accessed, so you need to go to the cloud console to configure it.
Since I didn't build a firewall separately, I just set it up here in Tencent. If it is a machine with a firewall installed, please execute the following statement.
firewall-cmd --add-port=13800/tcp —-permanent
firewall-cmd —-reload
10. Initial password, set Chinese environment
Wait two minutes to access your server ip: 13800.
The initial account is root, you can set the password as you like.
Pull to the bottom
Set Monday to be the first day of the week
11. Register a trumpet, and pass the review
In actual development, we definitely don't need root account to develop, we register a small account.
Although the actual registration is successful, you still can't log in with this number, because you have to use root to apply for the registration of the following account.
After doing this, you can visit normally.
end.
In the next article, we need to modify the configuration of gitlab and use gitlab-runner, so that our gitlab can also follow the ci/cd process, and officially start calling gitlab's api, is it not that difficult, next we will also enter nest
It has been officially developed, and this is the case this time. I hope to make progress with you.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。