Node and front-end project pagoda deployment

First you need a server

Purchase channels: Alibaba Cloud, Tencent Cloud, Baidu Cloud, Huawei Cloud

1. Take Alibaba Cloud as an example to purchase esc

Test servers can be temporarily purchased

2. Install the pagoda

Copy the public IP address and connect the account and password through Xshell

After connecting, visit the Pagoda official website to download the Pagoda panel, a free and versatile server operation and maintenance software.

Find the system you bought and install it

Enter the command into Xshell or other tools and wait for installation. After installation, a pagoda address and account password will appear for access.

3. Pagoda operation

1. Import the database

1. Find your database, mysql/mongodb in the software store

Click Settings to modify MongoDB configuration

bindIp changed from 127.0.0.1 to 0.0.0.0, releasing IP restrictions

Authorization is disabled by default. If permission verification is required, change it to enabled.

After accessing the ip+port, you can see that mongodb has started successfully.

It looks like you are trying to access MongoDB over HTTP on the native driver port.

2. You can find your own database and import the previous data.

After importing, you can set the account password and then the code needs to be connected.

2. Run the node service

Import the server project

You can delete nodemodules in advance and move them in and install node on npm i

Find the code to connect to the database

mongoose.connect("mongodb://账号:密码@ip:端口/数据库名称")

After importing, you need to write a piece of code to monitor the port operation.

const serve_ip = 8889;
const serve_port = 8889;
const port = serve_port;
app.listen(port, () => {
  console.log(`http://${serve_ip}:${port}`);
})

Remember to open port 8889 on Pagoda and Alibaba Cloud (all ports used need to be open)

Alibaba Cloud goes to the security group to add a custom port.

After that, you can run npm start and run the following to access it. You can access your own service through http.

3.pm2 hosting

Install pm2 in the software store to add projects

The startup file is subject to your own. My project is started based on www.

Just click Save

If there are errors during operation, you can view the logs

After that, it can be suspended forever. If there are changes, you need to restart the server.

4. Deploy the front-end

The front-end can currently be packaged directly in the form of /api because the front-end can solve the problem during development and the back-end needs to be solved by the nginx agent after deployment.

After packaging, put the dist file into the directory

1. Add a site

Add your own IP address. The default is 80. No processing is required.

The dist of the file in the root directory will automatically match the index file.

Set the corresponding website directory

Later, you need to add it to the configuration file to solve the cross-domain and refresh 404 issues.

It is mainly used to handle the front-end using a proxy. The cross-domain front-end does not need to modify the path, such as the beginning of the api.

 # nginx 跨域代理
    location ^~/api/ {
        proxy_pass ip:8889/;
    }

    # nginx 解决history路由刷新  404代理
    location / {
        try_files $uri $uri/ @router;
        index index.html;
    }

    location @router {
      rewrite ^.*$ /index.html last;
    }

node code solves cross-domain issues:

app.all('*', function (req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header('Access-Control-Allow-Headers', ['mytoken','Content-Type']);
	next();
});

Guess you like

Origin blog.csdn.net/m0_46846526/article/details/132483416