Javascript 如何从外部URL获取代码并在NodeJS中执行

Javascript 如何从外部URL获取代码并在NodeJS中执行,javascript,node.js,get,Javascript,Node.js,Get,我需要创建一个NodeJS脚本,该脚本将从我的VPS上托管的脚本自动更新。要做到这一点,我需要从我的VPS获取代码并将其发送到客户机,客户机将像在客户机中一样执行代码。我不知道如何做到这一点。。。 我所拥有的: 客户端: WebSocket = require('ws') ws = new WebSocket('ws://localhost:8720'); var getinit_init_key = "jN*&gbhh*&G8ihae8rwgh78g&*G&*G

我需要创建一个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);
服务器:

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文件放在外部源代码中,并使用
标记将代码导入客户端代码中,就像可以从cdn中使用jquery.js一样

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>


导入后,您可以直接使用该js文件中的方法。

我使用的是NodeJS,而不是HTML网页。您可以将其放入requireI中。我发现了一些有用的东西:var http=require('http')、vm=require('vm')、concat=require('concat-stream');http.get({host:'localhost',端口:80,路径:'/foo.js'},函数(res){res.setEncoding('utf8');res.pipe(concat({encoding:'string'},函数(ret){vm.runinthiscoxt(ret,/foo.js');}));});但这只作为普通的JavaScrpt执行代码,而不是作为节点执行代码