针对单个正在运行的实例启动多个 REPL 实例


¥Starting multiple REPL instances against a single running instance

可以针对共享单个 global 对象但具有单独 I/O 接口的单个​​运行 Node.js 实例创建和运行多个 REPL 实例。

¥It is possible to create and run multiple REPL instances against a single running instance of Node.js that share a single global object but have separate I/O interfaces.

例如,以下示例在 stdin、Unix 套接字和 TCP 套接字上提供单独的 REPL:

¥The following example, for instance, provides separate REPLs on stdin, a Unix socket, and a TCP socket:

const net = require('node:net');
const repl = require('node:repl');
let connections = 0;

repl.start({
  prompt: 'Node.js via stdin> ',
  input: process.stdin,
  output: process.stdout,
});

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via Unix socket> ',
    input: socket,
    output: socket,
  }).on('exit', () => {
    socket.end();
  });
}).listen('/tmp/node-repl-sock');

net.createServer((socket) => {
  connections += 1;
  repl.start({
    prompt: 'Node.js via TCP socket> ',
    input: socket,
    output: socket,
  }).on('exit', () => {
    socket.end();
  });
}).listen(5001); 

从命令行运行此应用将在标准输入上启动 REPL。其他 REPL 客户端可以通过 Unix 套接字或 TCP 套接字连接。例如,telnet 可用于连接 TCP 套接字,而 socat 可用于连接 Unix 和 TCP 套接字。

¥Running this application from the command line will start a REPL on stdin. Other REPL clients may connect through the Unix socket or TCP socket. telnet, for instance, is useful for connecting to TCP sockets, while socat can be used to connect to both Unix and TCP sockets.

通过从基于 Unix 套接字的服务器而不是标准输入启动 REPL,可以连接到长时间运行的 Node.js 进程而无需重新启动它。

¥By starting a REPL from a Unix socket-based server instead of stdin, it is possible to connect to a long-running Node.js process without restarting it.

有关在 net.Servernet.Socket 实例上运行 "full-featured" (terminal) REPL 的示例,请参阅:https://gist.github.com/TooTallNate/2209310

¥For an example of running a "full-featured" (terminal) REPL over a net.Server and net.Socket instance, see: https://gist.github.com/TooTallNate/2209310.

有关在 curl(1) 上运行 REPL 实例的示例,请参阅:https://gist.github.com/TooTallNate/2053342

¥For an example of running a REPL instance over curl(1), see: https://gist.github.com/TooTallNate/2053342.