diff --git a/app.js b/app.js new file mode 100644 index 0000000..431bef7 --- /dev/null +++ b/app.js @@ -0,0 +1,57 @@ +const express = require('express'); +const path = require('path'); +const os = require('os'); +const exec = require('child_process').exec; +const app = express(); + +const arr = []; + +app.all('*', function(req, res, next) { + res.header('Access-Control-Allow-Origin', 'http://localhost:8080'); + res.header('Access-Control-Allow-Headers', 'content-type'); + res.header('Access-Control-Allow-Methods', 'DELETE,PUT,POST,GET,OPTIONS'); + if (req.method.toLowerCase() == 'options') res.send(200); + else { + next(); + } +}); + +app.get('/', (req, res) => { + res.sendFile(__dirname + '/index.html'); +}); + +app.get('/device', (req, res) => { + console.log(0); + res.send({ + cpus: os.cpus(), + os: `${os.platform()} -- ${os.release()} -- ${os.type()}`, + memory: os.totalmem(), + process: arr + }); +}); + +app.listen(8080, () => { + console.log(`app listening at port 8080`); +}); + +const cmd = process.platform == 'win32' ? 'tasklist' : 'ps aux'; + +exec(cmd, function(err, stdout, stderr) { + if (err) { + return console.log(err); + } + stdout.split('\n').filter(function(line) { + let p = line.trim().split(/\s+/), + pname = p[0], + pid = p[1]; + if (p[4]) { + var pmem = p[4].split(',').join(''); + } + if (Number(pid) > 0) { + arr.push([pname, pid, pmem]); + } + }); + arr.sort(function(a, b) { + return Number(b[2]) - Number(a[2]); + }); +}); diff --git a/bin/hotnode.js b/bin/hotnode.js new file mode 100644 index 0000000..eb57b29 --- /dev/null +++ b/bin/hotnode.js @@ -0,0 +1,19 @@ +const fs = require('fs'); +const path = require('path'); +const exec = require('child_process').exec; +const file = path.resolve('./') + '\\app.js'; + +//第一次启动 , 记录pid +let pNode = exec('node app.js'); +console.log(pNode); +console.log(`服务已启动 , 当前id : ${pNode.pid}`); +fs.watch(file, (eventType, filename) => { + if (filename && eventType === 'change') { + console.log(` ${filename} 已更改 , 重启中..`); + pNode.kill(); + pNode = exec('node app.js'); + console.log(` 新的id : ${pNode.pid} `); + } else { + console.log('没change , 或没文件名'); + } +}); diff --git a/index.html b/index.html new file mode 100644 index 0000000..833fd54 --- /dev/null +++ b/index.html @@ -0,0 +1,33 @@ + + + + + + + Get server status + + +

Get server status

+ + + + diff --git "a/node\350\216\267\345\217\226\347\263\273\347\273\237\344\277\241\346\201\257.png" "b/node\350\216\267\345\217\226\347\263\273\347\273\237\344\277\241\346\201\257.png" new file mode 100644 index 0000000..5651f6a Binary files /dev/null and "b/node\350\216\267\345\217\226\347\263\273\347\273\237\344\277\241\346\201\257.png" differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..8ff2fad --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "server-test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.17.1" + }, + "devDependencies": { + "nodemon": "^1.19.1" + } +} diff --git "a/\351\207\215\345\220\257.png" "b/\351\207\215\345\220\257.png" new file mode 100644 index 0000000..497752a Binary files /dev/null and "b/\351\207\215\345\220\257.png" differ