Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});
19 changes: 19 additions & 0 deletions bin/hotnode.js
Original file line number Diff line number Diff line change
@@ -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 , 或没文件名');
}
});
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Get server status</title>
</head>
<body>
<h2>Get server status</h2>

<script>
console.log('getting server status .. ');

const url = 'http://127.0.0.1:8080/device';

try {
(async () => {
let response = await fetch(url);
let data = await response.json();
console.log(data);
})();
} catch (e) {
console.error(e);
}

// fetch(url)
// .then((response) => response.json())
// .then((data) => console.log(data))
// .catch((e) => console.error( e));
</script>
</body>
</html>
Binary file added node获取系统信息.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Binary file added 重启.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.