官术网_书友最值得收藏!

The Read-Eval-Print Loop and executing a Node program

Node's REPL (Read-Eval-Print-Loop) represents the Node shell. To enter the shell prompt, enter Node via your terminal without passing a filename:

> node

You now have access to a running Node process, and may pass JavaScript commands to this process. For example, after entering 2+2 the shell would send 4 to stdout. Node's REPL is an excellent place to try out, debug, test, or otherwise play with JavaScript code.

Because the REPL is a native object, programs can also use instances as a context in which to run JavaScript interactively. For example, here we create our own custom function sayHello, add it to the context of a REPL instance, and start the REPL, emulating a Node shell prompt:

require('repl').start("> ").context.sayHello = function() {
    return "Hello"
};

Entering sayHello() at the prompt will result in Hello being sent to stdout.

Let's take everything we've learned in this chapter and create an interactive REPL which allows us to execute JavaScript on a remote server.

Create two files, repl_client.js and repl_server.js, using the following code, and run each in its own terminal window, such that both terminal windows are visible to you.

/*      repl_client.js   */
var net = require('net');
var sock = net.connect(8080);
process.stdin.pipe(sock);
sock.pipe(process.stdout);

/*      repl_server.js  */
var repl = require('repl')
var net = require('net')
net.createServer(function(socket) {
  repl
  .start({
    prompt : '> ',
    input    : socket,
    output  : socket,
    terminal  : true
  })
  .on('exit', function () {
    socket.end()
  })
}).listen(8080)

repl_client simply creates a new socket connection to port 8080 through net.connect, and pipes any data coming from stdin (your terminal) through that socket. Similarly, any data arriving from the socket is piped to stdout (your terminal). It should be clear that we have created a way to take input and send it via a socket to port 8080, listening for any data that the socket may send back to us.

repl_server closes the loop. We first create a new TCP (Transmission Control Protocol) server with net.createServer, binding to port 8080 via .listen. The callback passed to net.createServer will receive a reference to the bound socket. Within the enclosure of that callback we instantiate a new REPL instance, giving it a nice prompt (> here, but could be any string), indicating that it should both listen for input from, and broadcast output to, the passed socket reference, indicating that the socket data should be treated as terminal data (which has special encoding).

We can now type something like console.log("hello") into the client terminal, and see hello displayed.

To confirm that the execution of our JavaScript commands is occurring in the server instance, type console.log(process.argv) into the client, and the server will display an object containing the current process path, which will be repl_server.js.

It should be clear from this demonstration that we have created a way to remotely control Node processes. It is a short step from here to multi-node analytics tools, remote memory management, automatic server administration, and so forth.

主站蜘蛛池模板: 江口县| 江孜县| 合作市| 佛教| 东兰县| 乐清市| 横山县| 虹口区| 台前县| 金沙县| 东乡| 凤台县| 福贡县| 庆城县| 台湾省| 柏乡县| 台湾省| 巴彦淖尔市| 十堰市| 鹤山市| 贵港市| 上饶县| 建德市| 同江市| 宜兰县| 弥渡县| 广东省| 连江县| 积石山| 鄂托克旗| 洛南县| 长垣县| 文安县| 贡嘎县| 兖州市| 镇平县| 乌拉特前旗| 客服| 梁河县| 宁海县| 昌宁县|