我将如何从外部 URL 获取代码并在 NodeJS 中执行它
How would i get code from an external URL and execute it in NodeJS
我需要创建一个 NodeJS 脚本,该脚本将从我的 VPS 上托管的脚本自动更新。为此,我需要从我的VPS获取代码并将其发送到客户端,客户端将像在客户端中一样执行代码。我不知道如何实现这一点...我有什么:
客户:
WebSocket = require('ws')
ws = new WebSocket('ws://localhost:8720');
var getinit_init_key = "jN*&gbhh*&G8ihae8rwgh78g&*G&*G&GFUibg&GB*&GVBWG";
var getINITKey = JSON.stringify({ init: getinit_init_key, userip: 'server', reason: 'getUpdate' });
ws.on('open', function open() {
ws.send(getINITKey);
});
ws.on('message', function(data, flags) {
d = JSON.parse(data)
d.x();
});
服务器:
update1 = require('./socks.js') //socks.js is my code
update2 = update1.init
update = JSON.stringify({ x: update2.toString() });
var ws = require("nodejs-websocket");
var server = ws.createServer(function(conn) {
console.log("Got new connection!");
conn.on("text", function(data) {
try {
conn.sendText(update)
console.log(JSON.parse(update).x);
}
catch (error) {
console.log("RECOVERED FROM ERROR: " +error)
}
});
conn.on("close", function(code, reason) {
console.log('Sent Update to User');
});
conn.on("error", function(error) {
console.log("Recovered from Error");
});
}).listen(8720);
实际上,您可以将代码作为js文件放在外部源代码上,并使用<script>
标记在客户端代码中,您可以导入代码,例如如何从CDN使用jquery.js。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
导入后,您可以直接使用该 js 文件中的方法。
相关文章:
- 如何在javasciipt中添加单引号到字符串,该字符串将在nodeJS中作为mysql插入命令执行
- Nodejs无法杀死Node自身执行的外部windows可执行程序
- 全局安装的NodeJS npm模块不执行节点的主/bin JavaScript文件
- NodeJS-如何为模块分配构造函数.导出自执行函数
- nodejs'exec'命令不't执行'sed'命令正确
- 在nodejs中执行procces数组时保存数据
- 如何在调用 responde.end() 后执行 NodeJS/connect 中间件
- nodejs:setImmediate 回调在 setTimeout(fn, 0) 之后执行,与 nodejs 文档描述
- 在循环完成 nodejs 请求之前执行回调
- 如何使用angularjs,nodejs和mysql执行CRUD操作
- 为什么 NodeJS 不像浏览器那样执行构造函数
- Nodejs.订阅 EventEmitter.emits 并仅在特定条件下执行
- 在 Nodejs 中,如果你先调用 cb,那么这会导致函数的其余部分执行吗?
- Nodejs执行节点文件并获取其输出
- nodejs执行字符串变量
- nodeJS执行不工作的"cd "壳牌cmd
- 使用nodejs执行shell脚本命令
- NodeJS执行错误TypeError: Cannot read property 'host'的定义
- NodeJS - 执行 4 次连续收集计数的水线最佳方法
- Nodejs执行命令失败,没有有用的错误信息