A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includable in other programs. REPL provides a way to interactively run JavaScript and see the results. It can be used for debugging, testing, or just trying things out.node
交互式解釋器(REPL)既能夠做爲一個獨立的程序運行,也能夠很容易地包含在其餘程序中做爲總體程序的一部分使用。REPL爲運行JavaScript腳本與查看運行結果提供了一種交互方式,一般REPL交互方式能夠用於調試、測試以及試驗某種想法。express
By executing node
without any arguments from the command-line you will be dropped into the REPL. It has simplistic emacs line-editing.bash
在命令行中不帶任何參數地運行node
命令,就能夠進入REPL環境,在該環境下你能夠進行一些相似Emacs的行編輯操做。app
mjr:~$ node Type '.help' for options. > a = [ 1, 2, 3]; [ 1, 2, 3 ] > a.forEach(function (v) { ... console.log(v); ... }); 1 2 3
For advanced line-editors, start node with the environmental variable NODE_NO_READLINE=1
. This will start the REPL in canonical terminal settings which will allow you to use withrlwrap
.socket
爲了進行高級的行編輯操做,能夠設置環境變量NODE_NO_READLINE=1
並啓動node。這種狀況REPL會進入標準終端設置模式,這此模式下你可使用rlwrap
。ide
For example, you could add this to your bashrc file:oop
好比,你能夠把下列設置添加到你的bashrc文件中:測試
alias node="env NODE_NO_READLINE=1 rlwrap node"
Starts a REPL with prompt
as the prompt and stream
for all I/O. prompt
is optional and defaults to >
. stream
is optional and defaults to process.stdin
.ui
啓動一個REPL,使用prompt
做爲輸入提示符,在stream
上進行全部I/O操做。prompt
是可選參數,默認值爲>
,stream
也是可選參數,默認值爲process.stdin
。
Multiple REPLs may be started against the same running instance of node. Each will share the same global object but will have unique I/O.
一個node實例中能夠啓動多個REPL,它們共享相同的全局對象,但擁有各自獨立的I/O。
Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
下面是的例子展現分別在標準輸入,Unix套接字及TCP套接字上啓動的REPL示例:
var net = require("net"), repl = require("repl"); connections = 0; repl.start("node via stdin> "); net.createServer(function (socket) { connections += 1; repl.start("node via Unix socket> ", socket); }).listen("/tmp/node-repl-sock"); net.createServer(function (socket) { connections += 1; repl.start("node via TCP socket> ", socket); }).listen(5001);
Running this program from the command line will start a REPL on stdin. Other REPL clients may connect through the Unix socket or TCP socket. telnet
is useful for connecting to TCP sockets, and socat
can be used to connect to both Unix and TCP sockets.
在命令行中運行這段程序將首先使用標準輸入啓動REPL。其餘的REPL終端能夠經過Unix套接字或者TCP套接字進行鏈接。可以使用telnet
程序鏈接到TCP套接字,而socat
程序既能夠鏈接到Unix套接字也能夠鏈接鏈接到TCP套接字。
By starting a REPL from a Unix socket-based server instead of stdin, you can connect to a long-running node process without restarting it.
若要鏈接到一個長時間運行的node進程而無需重啓進程,你應該將REPL啓動在Unix套接字上,非不要將其啓動在標準輸出上。
Inside the REPL, Control+D will exit. Multi-line expressions can be input.
在REPL中,操做組合鍵Control+D能夠退出。能夠輸入多行表達式。
The special variable _
(underscore) contains the result of the last expression.
特殊變量_
(下劃線)包含了上一表達式的結果。
> [ "a", "b", "c" ] [ 'a', 'b', 'c' ] > _.length 3 > _ += 1 4
The REPL provides access to any variables in the global scope. You can expose a variable to the REPL explicitly by assigning it to the context
object associated with eachREPLServer
. For example:
在REPL能夠訪問全局做用域中的任何變量。爲了在REPL中訪問一個變量,你只要將此變量顯性地分配給相應REPLServer
的context
對象。示例以下:
// repl_test.js var repl = require("repl"), msg = "message"; repl.start().context.m = msg;
Things in the context
object appear as local within the REPL:
context
對象中的變量在REPL中看起來就像是本地變量。
mjr:~$ node repl_test.js > m 'message'
There are a few special REPL commands:
如下是另一些特殊的REPL命令:
.break
- While inputting a multi-line expression, sometimes you get lost or just don't care about completing it. .break
will start over. .break
- 當你輸入多行表達式,若是想放棄當前的輸入,能夠用.break
跳出。.clear
- Resets the context
object to an empty object and clears any multi-line expression. .clear
- 將context
重置爲空對象,並清空當前正在輸入的多行表達式。.exit
- Close the I/O stream, which will cause the REPL to exit. .exit
- 該命令用於關閉I/O流,並退出REPL。.help
- Show this list of special commands. .help
- 輸出特殊命令的列表。