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: _posts/2012-11-2-shell.md
+26-4Lines changed: 26 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,9 +40,9 @@ The output of your program might look something like this:
40
40
41
41
{% highlight javascript %}
42
42
foo
43
-
<Buffer 64>
43
+
<Buffer 61 73 64 0a>
44
44
bar
45
-
<Buffer 64>
45
+
<Buffer 62 61 72 0a>
46
46
{% endhighlight %}
47
47
48
48
You can see that we're printing out a `Buffer` object. That's because the `input` variable passed in to our `function (input) { ... }` callback does not contain the string value of your input directly.
@@ -70,6 +70,8 @@ command [args...]
70
70
We can use a handy regular expression to separate the arguments from the command: `/(\w+)(.*)/`. We'll then parse our arguments by splitting on white space.
71
71
72
72
{% highlight javascript %}
73
+
var stdin = process.openStdin();
74
+
73
75
stdin.on('data', function (input) {
74
76
var matches = input.toString().match(/(\w+)(.*)/);
75
77
var command = matches[1].toLowerCase();
@@ -124,7 +126,7 @@ To clarify what's happening above, here's sample output of executing the regular
124
126
input: 'cmd_name arg1 arg2'] // matches[4]
125
127
{% endhighlight %}
126
128
127
-
We are accessing ```matches[1]``` because it's the first group (groups are specified with the parenthesis). If you are unfamiliar with regular expressions, a good source to learn more is at [http://regular-expressions.info](http://www.regular-expressions.info/), in spite of (because of?) the awesome color scheme.
129
+
We are accessing ```matches[1]``` because it's the first group (groups are specified with the parenthesis). If you are unfamiliar with regular expressions, a good source to learn more is at [http://regular-expressions.info](http://www.regular-expressions.info/).
128
130
129
131
Now, jump back to your terminal and give our shell a try!
130
132
@@ -197,6 +199,26 @@ stdin.on('data', function (input) {
197
199
});
198
200
{% endhighlight %}
199
201
202
+
#### As a side note about our commands object
203
+
204
+
The commands object can get a bit hairy when we're nesting so many brackets deep. We can instead define our commands object like so:
205
+
206
+
{% highlight javascript %}
207
+
var commands = {};
208
+
209
+
commands['pwd'] = function () {
210
+
console.log(process.cwd());
211
+
};
212
+
213
+
commands['ls'] = function (args) {
214
+
fs.readdir(args[0] || process.cwd(), function (err, entries) {
215
+
entries.forEach(function (e) {
216
+
console.log(e);
217
+
});
218
+
});
219
+
};
220
+
{% endhighlight %}
221
+
200
222
## Interacting with HTTP: Downloading a File
201
223
202
224
Similarly to the `fs` module, Node.js also contains a core `http` module which can be used to act as a HTTP client or server. In the next lab you'll be creating a HTTP server, but for now, we'll focus on creating a simple `wget` command to download a file.
@@ -229,7 +251,7 @@ var commands = {
229
251
};
230
252
{% endhighlight %}
231
253
232
-
Let's talk about what's happening in that callback.
254
+
Let's talk about what's happening in the callback provided to `http.get` callback.
233
255
234
256
First, we're creating a [writable stream](http://nodejs.org/api/stream.html#stream_class_stream_writable) to our file system using [fs.createWriteStream](http://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options), named `file` (the second argument, or `'download'` by default). This will create or overwrite the file.
0 commit comments