Skip to content

Instantly share code, notes, and snippets.

@Kaiyuan
Last active February 28, 2020 02:31
Show Gist options
  • Save Kaiyuan/1fa0c255bd9d04b78242a4f252ac7e2c to your computer and use it in GitHub Desktop.
Save Kaiyuan/1fa0c255bd9d04b78242a4f252ac7e2c to your computer and use it in GitHub Desktop.
node 唤醒设备模块

Npm Install express body-parser wol silly-datetime

npm install express body-parser wol silly-datetime --sava

sudo npm install pm2 -g

sudo pm2 start /volume1/web/node/pm2_config.json
/**
* 首页文件
* */
//1. 导入express
var express = require('express');
var wol = require('./wol.js');
// 创建服务器
var app = new express();
app.use('/wol', wol);
//. 绑定端口
app.listen(5050);
{
"apps" : [{
"name" : "nodewol",
"script" : "/volume1/web/node/index.js",
"cwd" : "/volume1/web/node/",
"instances" : "1",
"log_date_format" : "YYYY-MM-DD HH:mm Z",
"log_file" : "/volume1/web/node/log/wol.log",
"error_file" : "/volume1/web/node/log/wol-err.log",
"out_file" : "/volume1/web/node/log/wol-out.log",
"watch" : true
}]
}
/**
* Post请求执行 shell 命令
* 只要向这个地址提交 {"mac":"设备 MAC 地址"}
* kaiyuan Hsie
* https://boxks.com
* */
//1. 导入express
var express = require('express');
// 解析器 需要安装 npm install -g body-parser
var bodyParser = require('body-parser');
// WOL 模块
var wolfun = require('wol');
// 时间模块
var sd = require('silly-datetime');
var nowTime=sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });
// 创建服务器
var router = express.Router();
router.post("*", urlencodedParser, function (request, response) {
//
if (!request.body) {
response.send("请提交设备 MAC 地址");
} else {
var thisMAC = request.body.mac;
wolfun.wake(thisMAC, function(error, postText){
if (error !== null) {
console.log('-wol- exec error: ' + error+' '+nowTime);
response.send('exec error: ' +error);
} else {
console.log("-wol- "+thisMAC+' '+postText+' '+nowTime);
response.send('已经叫咗电脑开机!');
}
});
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment