针对单个正在运行的实例启动多个 REPL 实例
【Starting multiple REPL instances against a single running instance】
可以针对单个运行中的 Node.js 实例创建并运行多个 REPL 实例,这些实例共享同一个 global 对象,但具有独立的输入/输出接口。
【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.Server 和 net.Socket 实例上运行“全功能”(terminal)REPL 的示例,请参见:
https://gist.github.com/TooTallNate/2209310。
有关在 curl(1) 上运行 REPL 实例的示例,请参见:
https://gist.github.com/TooTallNate/2053342。