Why am I writing this article

Last night, a young man sent me a letter asking me how to deploy a Node service and whether there was any relevant tutorial. I was a little shocked, so I asked him what he didn’t know, and he said he almost didn’t know anything. I think he must have looked for the relevant tutorial, but could not read or understand.

I asked him to write down what he didn’t understand, and I made a video, cover it from beginning to end. You are not a person, but the representative of a group. Since you do not understand, there will also be some people who do not understand, so I decided to start from the beginning to explain how to use Node to develop a service, deploy on the server, and use the domain name resolution.

Video Address:

Station B (Click to jump to)

Knowledge points involved

Node. js, KOA2, Koa-Router, PM2, nginx, Linux

Tutorial structure

Step 1: Write a Node service

There are several ways to write a Node service:

  • Write in native Node
  • Written in express
  • Written in koa2
  • An egg on the direct

Let’s write one here in koa2:

Create a project node-koa-pm2:

mkdir node-koa-pm2 && cd node-koa-pm2 && npm init -y
Copy the code

Install koA and KOA-router:

npm install koa koa-router --save
Copy the code

At this point, the project is successfully created, create app.js in the root directory:

const Koa = require('koa');
const app = new Koa();
const router = require('./api/test')
app.use(async (ctx, next) => {
    const start = Date.now();
    await next();
    const ms = Date.now() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});
app.use(router.routes())
app.listen(3000);
Copy the code

Test.js = test.js; test.js = test.js;

const Router = require('koa-router'); const router = new Router(); router .get('/api/', (ctx, next) => { ctx.body = 'Hello World! '; }) .get('/api/test', (ctx, next) => { ctx.body = { msg:'here is test', query:ctx.query, queryStr:ctx.querystring, } }) .post('/api/users', (ctx, next) => { ctx.body = 'here is users'; }) .all('/api/users/:id', (ctx, next) => { // ... }); module.exports = routerCopy the code

The current directory structure is shown as follows:

Start the service in the root directory:

node app.js
Copy the code

Browser visit: http://127.0.0.1:3000/api/test? Aa =1 effect is as follows:

At this point, the first step of node service creation is complete!

Step 2: Have a server (Node environment)

Purchase: Ali Cloud, Tencent Yun, Baidu Cloud, Huawei Cloud Personal: 500 YUAN/year (recommended double eleven PIN group) System: centos 7

Install the node environment: blog.csdn.net/u014726163/…

Upload code to server: FTP, git, rz

npm i
Copy the code

Try to start the service:

node app.js
Copy the code

Curl server local access

The curl http://127.0.0.1:3000/api/test? aa=1Copy the code

Returns on success:

{"msg":"here is test","query":{"aa":"1"},"queryStr":"aa=1"}
Copy the code

Step 3: Install and use the PM2

npm install -g pm2
Copy the code

Start the Node service with pM2:

pm2 start app.js -i max -n node-koa-pm2
Copy the code

Curl server local access (pM2 startup) :

The curl http://127.0.0.1:3000/api/test? aa=1Copy the code

Returns if successful (after PM2 is started) :

{"msg":"here is test","query":{"aa":"1"},"queryStr":"aa=1"}
Copy the code

Pm2 common name:

Jump (Douban)

Step 4: Install nginx

Installation method:

Yum install:Copy the code
yum install nginx
Copy the code

View version:

nginx -v
Copy the code

Example returned (error) :

Nginx version: nginx / 1.16.1Copy the code

Step 5: Configure nginx

Domain name resolution:

Use my Aliyun domain name: node.lijsister.cn

Parsing to brother’s server: 182.61.31.56

The configuration screenshot is as follows:

Reverse proxy configuration:

The default configuration file for nginx installation is /etc/nginx/conf.d/

Go to the configuration file directory:

cd /etc/nginx/conf.d/
Copy the code

Create a configuration file:

touch node.lijicheng.cn.80.conf
Copy the code

Paste the code into the configuration file:

server { listen 80; server_name node.lijicheng.cn; root html; index index.html index.htm; location /api/ { root /usr/share/nginx/html; index index.html index.htm; Proxy_pass http://127.0.0.1:3000; } location / {proxy_pass http://127.0.0.1:7001; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code

Check whether the configuration file is qualified:

nginx -t
Copy the code

If qualified, the following output will be generated:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Copy the code

Nginx configuration has no syntax problems.

nginx -s reload
Copy the code

If the following error occurs:

nginx: [error] invalid PID number "" in "/run/nginx.pid"
Copy the code

Need to reload the configuration file:

nginx -c /etc/nginx/nginx.conf
Copy the code

Restart nginx at this point:

nginx -s reload
Copy the code

Theoretically successful, if there are mistakes, but also depends on the specific wrong, and then targeted solution

At this time by domain access: click here to visit: http://node.lijicheng.cn:3000/api/test/?t=1

Theoretically, there will be a return, but because my domain name is Ali Cloud and the server is Baidu Cloud, Baidu rejected the request, and the domain name of Baidu Cloud will be resolved to the server later (to be continued).

Read here, maybe you already understand, maybe you still need questions, welcome to comment, you can also add my wechat, there may be a group behind

The domain name is on file

A month passed, after the efforts of the little brother, his domain name finally put on record, put on record after I put the domain name to replace after restart Nginx ran.

Video Address (Station B)

The address of success: node. NFCMDX. Top/API/test /? T…