forked from workshopper/learnyounode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.html
300 lines (265 loc) · 11.3 KB
/
repl.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>REPL Node.js v0.10.15 Manual & Documentation</title>
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/sh.css">
<link rel="canonical" href="http://nodejs.org/api/repl.html">
</head>
<body class="alt apidoc" id="api-section-repl">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
<img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js">
</a>
</div>
<div id="content" class="clearfix">
<div id="column2" class="interior">
<ul>
<li><a href="/" class="home">Home</a></li>
<li><a href="/download/" class="download">Download</a></li>
<li><a href="/about/" class="about">About</a></li>
<li><a href="http://npmjs.org/" class="npm">npm Registry</a></li>
<li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li>
<li><a href="http://blog.nodejs.org" class="blog">Blog</a></li>
<li><a href="/community/" class="community">Community</a></li>
<li><a href="/logos/" class="logos">Logos</a></li>
<li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li>
</ul>
<p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p>
</div>
<div id="column1" class="interior">
<header>
<h1>Node.js v0.10.15 Manual & Documentation</h1>
<div id="gtoc">
<p>
<a href="index.html" name="toc">Index</a> |
<a href="all.html">View on single page</a> |
<a href="repl.json">View as JSON</a>
</p>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li><a href="#repl_repl">REPL</a><ul>
<li><a href="#repl_repl_start_options">repl.start(options)</a><ul>
<li><a href="#repl_event_exit">Event: 'exit'</a></li>
</ul>
</li>
<li><a href="#repl_repl_features">REPL Features</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>REPL<span><a class="mark" href="#repl_repl" id="repl_repl">#</a></span></h1>
<p>A Read-Eval-Print-Loop (REPL) is available both as a standalone program and
easily includable in other programs. The REPL provides a way to interactively
run JavaScript and see the results. It can be used for debugging, testing, or
just trying things out.
</p>
<p>By executing <code>node</code> without any arguments from the command-line you will be
dropped into the REPL. It has simplistic emacs line-editing.
</p>
<pre><code>mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v) {
... console.log(v);
... });
1
2
3</code></pre>
<p>For advanced line-editors, start node with the environmental variable
<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical
terminal settings which will allow you to use with <code>rlwrap</code>.
</p>
<p>For example, you could add this to your bashrc file:
</p>
<pre><code>alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
<h2>repl.start(options)<span><a class="mark" href="#repl_repl_start_options" id="repl_repl_start_options">#</a></span></h2>
<p>Returns and starts a <code>REPLServer</code> instance. Accepts an "options" Object that
takes the following values:
</p>
<ul>
<li><p><code>prompt</code> - the prompt and <code>stream</code> for all I/O. Defaults to <code>> </code>.</p>
</li>
<li><p><code>input</code> - the readable stream to listen to. Defaults to <code>process.stdin</code>.</p>
</li>
<li><p><code>output</code> - the writable stream to write readline data to. Defaults to
<code>process.stdout</code>.</p>
</li>
<li><p><code>terminal</code> - pass <code>true</code> if the <code>stream</code> should be treated like a TTY, and
have ANSI/VT100 escape codes written to it. Defaults to checking <code>isTTY</code>
on the <code>output</code> stream upon instantiation.</p>
</li>
<li><p><code>eval</code> - function that will be used to eval each given line. Defaults to
an async wrapper for <code>eval()</code>. See below for an example of a custom <code>eval</code>.</p>
</li>
<li><p><code>useColors</code> - a boolean which specifies whether or not the <code>writer</code> function
should output colors. If a different <code>writer</code> function is set then this does
nothing. Defaults to the repl's <code>terminal</code> value.</p>
</li>
<li><p><code>useGlobal</code> - if set to <code>true</code>, then the repl will use the <code>global</code> object,
instead of running scripts in a separate context. Defaults to <code>false</code>.</p>
</li>
<li><p><code>ignoreUndefined</code> - if set to <code>true</code>, then the repl will not output the
return value of command if it's <code>undefined</code>. Defaults to <code>false</code>.</p>
</li>
<li><p><code>writer</code> - the function to invoke for each command that gets evaluated which
returns the formatting (including coloring) to display. Defaults to
<code>util.inspect</code>.</p>
</li>
</ul>
<p>You can use your own <code>eval</code> function if it has following signature:
</p>
<pre><code>function eval(cmd, context, filename, callback) {
callback(null, result);
}</code></pre>
<p>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.
</p>
<p>Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
</p>
<pre><code>var net = require("net"),
repl = require("repl");
connections = 0;
repl.start({
prompt: "node via stdin> ",
input: process.stdin,
output: process.stdout
});
net.createServer(function (socket) {
connections += 1;
repl.start({
prompt: "node via Unix socket> ",
input: socket,
output: socket
}).on('exit', function() {
socket.end();
})
}).listen("/tmp/node-repl-sock");
net.createServer(function (socket) {
connections += 1;
repl.start({
prompt: "node via TCP socket> ",
input: socket,
output: socket
}).on('exit', function() {
socket.end();
});
}).listen(5001);</code></pre>
<p>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. <code>telnet</code> is useful
for connecting to TCP sockets, and <code>socat</code> can be used to connect to both Unix and
TCP sockets.
</p>
<p>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.
</p>
<p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
a <code>net.Server</code> and <code>net.Socket</code> instance, see: <a href="https://gist.github.com/2209310">https://gist.github.com/2209310</a>
</p>
<p>For an example of running a REPL instance over <code>curl(1)</code>,
see: <a href="https://gist.github.com/2053342">https://gist.github.com/2053342</a>
</p>
<h3>Event: 'exit'<span><a class="mark" href="#repl_event_exit" id="repl_event_exit">#</a></span></h3>
<p><code>function () {}</code>
</p>
<p>Emitted when the user exits the REPL in any of the defined ways. Namely, typing
<code>.exit</code> at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
to signal "end" on the <code>input</code> stream.
</p>
<p>Example of listening for <code>exit</code>:
</p>
<pre><code>r.on('exit', function () {
console.log('Got "exit" event from repl!');
process.exit();
});</code></pre>
<h2>REPL Features<span><a class="mark" href="#repl_repl_features" id="repl_repl_features">#</a></span></h2>
<!-- type=misc -->
<p>Inside the REPL, Control+D will exit. Multi-line expressions can be input.
Tab completion is supported for both global and local variables.
</p>
<p>The special variable <code>_</code> (underscore) contains the result of the last expression.
</p>
<pre><code>> [ "a", "b", "c" ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4</code></pre>
<p>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 <code>context</code> object
associated with each <code>REPLServer</code>. For example:
</p>
<pre><code>// repl_test.js
var repl = require("repl"),
msg = "message";
repl.start("> ").context.m = msg;</code></pre>
<p>Things in the <code>context</code> object appear as local within the REPL:
</p>
<pre><code>mjr:~$ node repl_test.js
> m
'message'</code></pre>
<p>There are a few special REPL commands:
</p>
<ul>
<li><code>.break</code> - While inputting a multi-line expression, sometimes you get lost
or just don't care about completing it. <code>.break</code> will start over.</li>
<li><code>.clear</code> - Resets the <code>context</code> object to an empty object and clears any
multi-line expression.</li>
<li><code>.exit</code> - Close the I/O stream, which will cause the REPL to exit.</li>
<li><code>.help</code> - Show this list of special commands.</li>
<li><code>.save</code> - Save the current REPL session to a file<blockquote>
<p>.save ./file/to/save.js</p>
</blockquote>
</li>
<li><code>.load</code> - Load a file into the current REPL session.<blockquote>
<p>.load ./file/to/load.js</p>
</blockquote>
</li>
</ul>
<p>The following key combinations in the REPL have these special effects:
</p>
<ul>
<li><code><ctrl>C</code> - Similar to the <code>.break</code> keyword. Terminates the current
command. Press twice on a blank line to forcibly exit.</li>
<li><code><ctrl>D</code> - Similar to the <code>.exit</code> keyword.</li>
</ul>
</div>
</div>
</div>
<div id="footer">
<a href="http://joyent.com" class="joyent-logo">Joyent</a>
<ul class="clearfix">
<li><a href="/">Node.js</a></li>
<li><a href="/download/">Download</a></li>
<li><a href="/about/">About</a></li>
<li><a href="http://npmjs.org/">npm Registry</a></li>
<li><a href="http://nodejs.org/api/">Docs</a></li>
<li><a href="http://blog.nodejs.org">Blog</a></li>
<li><a href="/community/">Community</a></li>
<li><a href="/logos/">Logos</a></li>
<li><a href="http://jobs.nodejs.org/">Jobs</a></li>
<li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li>
</ul>
<p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.15/LICENSE">license</a>.</p>
</div>
<script src="../sh_main.js"></script>
<script src="../sh_javascript.min.js"></script>
<script>highlight(undefined, undefined, 'pre');</script>
<script>
window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']];
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.src = '//www.google-analytics.com/ga.js';
s.parentNode.insertBefore(g, s);
}(document, 'script'));
</script>
</body>
</html>