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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules
/npm-debug.log
.DS_Store
.todo.md
.idea/**
.vscode/
package-lock.json
46 changes: 46 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

var os = require("os");
var exec = require('child_process').exec;
var express = require('express');
var app = express();

var progressList = [];

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html')
})

app.get('/device',function(req, res){
res.send({
cpus: os.cpus(),
memory: os.totalmem(),
process: progressList
})
})

app.listen(8087,function(){
console.log('Server listen on 8087!')
})

const cmd = process.platform == 'win32' ? 'tasklist' : 'ps aux';

exec(cmd, function(error, stdout, stderr){
if(error){
console.log(`执行的错误:${error}`)
return;
}
stdout.split('\n').filter(function(line) {
let p = line.trim().split(/\s+/),
user = p[0],
pid = p[1],
pmem = p[4];

if (Number(pid) > 0) {
progressList.push([user, pid, pmem]);
}
});
progressList.sort(function(a, b) {
return Number(b[2]) - Number(a[2]);
});
console.log(`stderr: ${stderr}`);
})
47 changes: 47 additions & 0 deletions bin/hotloader
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var fs = require('fs');
var path = require("path");
var spawn = require('child_process').spawn;

class HotLoader {
constructor(args, extName, launcher) {
this.args = args;
this.extName = extName;
this.launcher = launcher;
this.passedArguments = this.args.slice(2);
}
run(){
fs.watch(
path.join(process.cwd(), this.passedArguments[0]),
(event, filename) => {
this.restartProcess();
}
);
return this.startProgress();
}
startProgress() {
console.log('文件启动===')
this.process = spawn(this.launcher, this.passedArguments, {
env: process.env
});
this.process.stdout.on("data", data => {
console.log(data.toString());
});
this.process.stderr.on("data", function(data) {
const errmessage = '----error----'+ data.toString();
console.log(errmessage)
});
}
restartProcess() {
if (this.process !== null) {
try {
this.process.kill("SIGKILL");
} catch (error) {
console.log("Exception: " + error.message, "bad");
}
}
console.log('文件重启====')
this.startProgress();
}
}

module.exports = HotLoader;
21 changes: 21 additions & 0 deletions bin/hotnode
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
var HotLoader, VERSION, args, loader, path;
path = require('path');
HotLoader = require('./hotloader');
VERSION = "0.0.1";
args = process.argv;
if (args.length > 2) {
if (args[2] === "-v" || args[2] === "--version") {
console.log("v" + VERSION);
} else {
loader = new HotLoader(args, 'js', 'node');
loader.run();
}
} else {
console.log("\nHotnode - Hot node is watching script for Nodejs\n");
console.log("Usage:");
console.log("hotcoffee [options] script.coffee [arguments] ");
console.log("Hotnode arguments:")
console.log(" -v=[--version] View version number of Hotnode")
console.log(" *.js Watch script Updates")
}
18 changes: 18 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!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>查看服务器的CPU、内存</title>
</head>
<body>
<h2>Server progress is starting!</h2>
<script>
fetch('/device')
.then((response) => response.json())
.then((data) => console.log(data))
.catch((e) => console.error( e));
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "homework-nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"hotnode": "node bin/hotnode",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/GGXXMM/homework-nodejs.git"
},
"author": "guoxinming",
"license": "ISC",
"bugs": {
"url": "https://github.com/GGXXMM/homework-nodejs/issues"
},
"homepage": "https://github.com/GGXXMM/homework-nodejs#readme",
"dependencies": {
"express": "^4.17.1"
}
}
Binary file added result/hotnode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added result/查看服务器的CPU和进程.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.