You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: locale/en/knowledge/REPL/how-to-create-a-custom-repl.md
+65-59Lines changed: 65 additions & 59 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,93 +8,99 @@ difficulty: 2
8
8
layout: knowledge-post.hbs
9
9
---
10
10
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:
12
12
13
-
repl.start(prompt, stream);
13
+
```js
14
+
var repl =require('repl')
14
15
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.
16
20
17
21
However, the repl is pretty flexible. Here's an example that shows this off:
18
22
19
-
#!/usr/bin/env node
23
+
```js
24
+
#!/usr/bin/env node
20
25
21
-
var net = require("net"),
22
-
repl = require("repl");
26
+
var net =require("net");
27
+
var repl =require("repl");
23
28
24
-
var mood = function () {
25
-
var m = [ "^__^", "-___-;", ">.<", "<_>" ];
26
-
return m[Math.floor(Math.random()*m.length)];
27
-
};
29
+
varmood=function () {
30
+
var m = [ "^__^", "-___-;", ">.<", "<_>" ];
31
+
return m[Math.floor(Math.random()*m.length)];
32
+
};
28
33
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);
36
41
37
-
console.log("Remote REPL started on port 5001.");
42
+
console.log("Remote REPL started on port 5001.");
38
43
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> ");
41
46
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
+
```
44
50
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*.
46
52
47
53
In addition, all objects in the global scope will also be accessible to your REPLs.
48
54
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
+
```
70
70
71
71
As may be seen, the `mood` function is usable within the local REPL, but the
72
72
`bonus` string is not. This is as expected.
73
73
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:
75
75
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
+
```
85
87
86
88
As you can see, the `mood` function is *also* available over telnet! In addition, so is "bonus".
87
89
88
90
As an interesting consequence of my actions, bonus is now also defined on the local REPL:
89
91
90
-
node::local> bonus
91
-
'UNLOCKED'
92
+
```shell
93
+
node::local> bonus
94
+
'UNLOCKED'
95
+
```
92
96
93
97
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:
94
98
95
-
node::local> var node = "AWESOME!"
99
+
```shell
100
+
node::local> var node = "AWESOME!"
96
101
97
-
node::remote> node
98
-
'AWESOME!'
102
+
node::remote> node
103
+
'AWESOME!'
104
+
```
99
105
100
106
As you can see, the node REPL is powerful and flexible.
0 commit comments