-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
195 lines (179 loc) · 4.01 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const express = require('express')
const pm2 = require('pm2')
const bodyParser = require('body-parser')// body-parser中间件来解析请求体
const os = require('os')
const shell = require('shelljs')
const homePath = os.homedir() // Maybe /Users/<name> on OSX, maybe /home/<name> on Linux and so on
const fs = require('fs')
const path = require('path')
const pm2LogPath = path.join(homePath, './.pm2/pm2.log')
const app = express()
app.use(express.static('dist'))
// 运用跨域的中间件
const allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*') // 自定义中间件,设置跨域需要的响应头。
next()
}
app.use(allowCrossDomain)
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// 设置模板
app.set('views', 'dist')
app.set('view engine', 'html')
app.engine('html', require('ejs-mate'))
// 设置路由
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!' })
})
//
app.get('/process-list', function (req, res) {
pm2.connect(function (err) {
if (err) {
res.send({
message: err,
data: [],
success: 0
})
process.exit(2)
}
pm2.list((err, list) => {
if (err) {
res.send({
message: err,
data: [],
success: 0
})
} else {
res.send({
message: 'success',
data: list,
success: 1
})
pm2.disconnect()
}
})
})
})
app.post('/restart-process-name', function (req, res) {
const name = req.body.name
pm2.connect(function (err) {
if (err) {
res.send({
message: err,
data: null,
success: 0
})
process.exit(2)
}
pm2.restart(name, (err, proc) => {
if (err) {
res.send({
message: err,
data: [],
success: 0
})
} else {
res.send({
message: 'success',
data: proc,
success: 1
})
pm2.disconnect()
}
})
})
})
app.post('/stop-process-name', function (req, res) {
const name = req.body.name
pm2.connect(function (err) {
if (err) {
res.send({
message: err,
data: null,
success: 0
})
process.exit(2)
}
pm2.stop(name, (err, proc) => {
if (err) {
res.send({
message: err,
data: null,
success: 0
})
} else {
res.send({
message: 'success',
data: proc,
success: 1
})
pm2.disconnect()
}
})
})
})
app.post('/delete-process-name', function (req, res) {
const name = req.body.name
pm2.connect(function (err) {
if (err) {
res.send({
message: err,
data: null,
success: 0
})
process.exit(2)
}
pm2.delete(name, (err, proc) => {
if (err) {
res.send({
message: err,
data: null,
success: 0
})
} else {
res.send({
message: 'success',
data: proc,
success: 1
})
pm2.disconnect()
}
})
})
})
app.post('/flush-logs', function (req, res) {
shell.exec('pm2 flush', function (code, stdout, stderr) {
res.send({
message: 'success',
code,
data: stdout || stderr,
success: 1
})
})
})
app.post('/get-logs', function (req, res) {
const reqPath = req.body.path
const logPath = reqPath || pm2LogPath
fs.readFile(logPath, { encoding: 'utf-8' }, function (err, data) {
if (!err) {
res.send({
message: 'success',
data: data,
success: 1
})
} else {
res.send({
message: 'failed',
data: err,
success: 0
})
}
})
})
const server = app.listen(3002, function () {
const host = server.address().address
const port = server.address().port
console.log('Example app listening at ', host, port)
})