Skip to content

Commit 6df81ca

Browse files
jonchurchZYSzys
authored andcommitted
Update knowledge/how to create a custom repl (#2157)
Updates to knowledge article **[how to create a custom repl](https://github.com/nodejs/nodejs.org/blob/master/locale/en/knowledge/REPL/how-to-use-nodejs-repl.md)**. Per #1977 (comment). This one didn't need much in my opinion. I updated the code block formatting and tweaked some phrasing. I did not add section headers to this one. * Update code fence syntax * Replace first person use of "I" with "you" cc @keywordnew @fhemberger Co-authored-by: <maledong_github@outlook.com>
1 parent 96b4322 commit 6df81ca

1 file changed

Lines changed: 65 additions & 59 deletions

File tree

locale/en/knowledge/REPL/how-to-create-a-custom-repl.md

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,99 @@ difficulty: 2
88
layout: knowledge-post.hbs
99
---
1010

11-
Node allows users to create their own REPLs with the [repl module](https://nodejs.org/docs/v0.4.10/api/repl.html). Its basic use looks like this:
11+
Node.js allows users to create their own REPLs with the [repl module](https://nodejs.org/api/repl.html). Its basic use looks like this:
1212

13-
repl.start(prompt, stream);
13+
```js
14+
var repl = require('repl')
1415

15-
`prompt` is a string that's used for the prompt of your REPL and defaults to "> ". `stream` is the stream that the repl listens on and defaults to `process.stdin`. When you run `node` from the command prompt, what it's doing in the background is running `repl.start()` to give you the standard REPL.
16+
repl.start(prompt, stream);
17+
```
18+
19+
Above, `prompt` is a string that's used for the prompt of your REPL (which defaults to "> ") and `stream` is the stream that the repl listens on, defaulting to `process.stdin`. When you run the standalone `node` REPL from the command prompt, what it's doing in the background is running `repl.start()` to give you the standard REPL.
1620

1721
However, the repl is pretty flexible. Here's an example that shows this off:
1822

19-
#!/usr/bin/env node
23+
```js
24+
#!/usr/bin/env node
2025

21-
var net = require("net"),
22-
repl = require("repl");
26+
var net = require("net");
27+
var repl = require("repl");
2328

24-
var mood = function () {
25-
var m = [ "^__^", "-___-;", ">.<", "<_>" ];
26-
return m[Math.floor(Math.random()*m.length)];
27-
};
29+
var mood = function () {
30+
var m = [ "^__^", "-___-;", ">.<", "<_>" ];
31+
return m[Math.floor(Math.random()*m.length)];
32+
};
2833

29-
//A remote node repl that you can telnet to!
30-
net.createServer(function (socket) {
31-
var remote = repl.start("node::remote> ", socket);
32-
//Adding "mood" and "bonus" to the remote REPL's context.
33-
remote.context.mood = mood;
34-
remote.context.bonus = "UNLOCKED";
35-
}).listen(5001);
34+
//A remote node repl that you can telnet to!
35+
net.createServer(function (socket) {
36+
var remote = repl.start("node::remote> ", socket);
37+
//Adding "mood" and "bonus" to the remote REPL's context.
38+
remote.context.mood = mood;
39+
remote.context.bonus = "UNLOCKED";
40+
}).listen(5001);
3641

37-
console.log("Remote REPL started on port 5001.");
42+
console.log("Remote REPL started on port 5001.");
3843

39-
//A "local" node repl with a custom prompt
40-
var local = repl.start("node::local> ");
44+
//A "local" node repl with a custom prompt
45+
var local = repl.start("node::local> ");
4146

42-
// Exposing the function "mood" to the local REPL's context.
43-
local.context.mood = mood;
47+
// Exposing the function "mood" to the local REPL's context.
48+
local.context.mood = mood;
49+
```
4450

45-
This script creates *two* REPLs: One is normal excepting for its custom prompt, but the *other* is exposed via the net module so I can telnet to it! In addition, it uses the `context` property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other *doesn't really work*.
51+
This script creates *two* REPLs: One is normal excepting for its custom prompt, but the *other* is exposed via the net module so you can telnet to it! In addition, it uses the `context` property to expose the function "mood" to both REPLs, and the "bonus" string to the remote REPL only. As you will see, this approach of trying to expose objects to one REPL and not the other *doesn't really work*.
4652

4753
In addition, all objects in the global scope will also be accessible to your REPLs.
4854

49-
Here's what happens when I run the script:
50-
51-
$ node repl.js
52-
Remote REPL started on port 5001.
53-
node::local> .exit
54-
^Cjosh@pidgey:/tmp/telnet$ node repl.js
55-
Remote REPL started on port 5001.
56-
node::local> mood()
57-
'^__^'
58-
node::local> bonus
59-
ReferenceError: bonus is not defined
60-
at [object Context]:1:1
61-
at Interface.<anonymous> (repl.js:171:22)
62-
at Interface.emit (events.js:64:17)
63-
at Interface._onLine (readline.js:153:10)
64-
at Interface._line (readline.js:408:8)
65-
at Interface._ttyWrite (readline.js:585:14)
66-
at ReadStream.<anonymous> (readline.js:73:12)
67-
at ReadStream.emit (events.js:81:20)
68-
at ReadStream._emitKey (tty_posix.js:307:10)
69-
at ReadStream.onData (tty_posix.js:70:12)
55+
Here's what happens when you run the script:
56+
57+
```shell
58+
$ node repl.js
59+
Remote REPL started on port 5001.
60+
node::local> .exit
61+
# <ctrl>-C
62+
63+
$ node repl.js
64+
Remote REPL started on port 5001.
65+
node::local> mood()
66+
'^__^'
67+
node::local> bonus
68+
ReferenceError: bonus is not defined
69+
```
7070

7171
As may be seen, the `mood` function is usable within the local REPL, but the
7272
`bonus` string is not. This is as expected.
7373

74-
Now, here's what happens when I try to telnet to port 5001:
74+
Now, here's what happens when you try to telnet to port 5001:
7575

76-
josh@pidgey:/tmp/telnet$ telnet localhost 5001
77-
Trying ::1...
78-
Trying 127.0.0.1...
79-
Connected to localhost.
80-
Escape character is '^]'.
81-
node::remote> mood()
82-
'>.<'
83-
node::remote> bonus
84-
'UNLOCKED'
76+
```shell
77+
$ telnet localhost 5001
78+
Trying ::1...
79+
Trying 127.0.0.1...
80+
Connected to localhost.
81+
Escape character is '^]'.
82+
node::remote> mood()
83+
'>.<'
84+
node::remote> bonus
85+
'UNLOCKED'
86+
```
8587

8688
As you can see, the `mood` function is *also* available over telnet! In addition, so is "bonus".
8789

8890
As an interesting consequence of my actions, bonus is now also defined on the local REPL:
8991

90-
node::local> bonus
91-
'UNLOCKED'
92+
```shell
93+
node::local> bonus
94+
'UNLOCKED'
95+
```
9296

9397
It seems we "unlocked" the `bonus` string on the local REPL as well. As it turns out, any variables created in one REPL are also available to the other:
9498

95-
node::local> var node = "AWESOME!"
99+
```shell
100+
node::local> var node = "AWESOME!"
96101

97-
node::remote> node
98-
'AWESOME!'
102+
node::remote> node
103+
'AWESOME!'
104+
```
99105

100106
As you can see, the node REPL is powerful and flexible.

0 commit comments

Comments
 (0)