Create a node server in vue scaffolding

Create a server through node

1 At the same level as src, create a server folder to place the node server

2 Open the terminal of the server folder and enter npm i  to initialize

3 After the initialization is complete, enter  the npm i express -S command line

4 Put the dist folder generated after packaging into the server folder

5 In the server folder, create app.js  and enter the following code

const express = require('express')

const app = express()

app.use(express.static('./dist'))

app.listen(8998,()=>{   //8998代表着端口号
    console.log("server running at http://127.0.0.1:8998")
})

Note: At this time and the following compression process, two terminals must also be opened, one is the terminal of the current node server, and the other is the terminal running the code

Enable gzip compression

1 In the terminal of the server folder, enter the command  cnpm i compression -D

2 In app.js, add the following code

const compression = require('compression')

app.use(compression())

Use pm2 to manage applications

After using pm2 management, there is no need to open two terminals, even if you close the terminal of node, as long as you don’t enter the command to close the server, you can still use the server

1 In the terminal of the server folder, enter the command line cnpm i pm2 -g 

2 Enter the command line  cpm2 start app.js --name aaa to start the project   

3 aaa is the custom name for this project, which can be changed at will, the command line is as follows

pm2 ls View project list command
pm2 restart aaa restart project
pm2 stop aaa stop project
pm2 delete aaa delete item

Guess you like

Origin blog.csdn.net/hjdjhh/article/details/122983014