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
+9-7Lines changed: 9 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ tags:
7
7
8
8
# A Node.js Shell
9
9
10
-
In this lab we'll put together a simple shell. We'll interact with the file system and learn some useful facets of the JavaScript programming language.
10
+
In this lab we'll put together a simple shell. We'll interact with the file system and network while learning some useful features of JavaScript.
11
11
12
12
-[Working with standard input](#working_with_standard_input)
13
13
-[Implementing a print working directory command](#implementing_a_print_working_directory_command)
@@ -18,7 +18,7 @@ In this lab we'll put together a simple shell. We'll interact with the file syst
18
18
19
19
Commands will be provided to our shell through the process' standard input. By default, Node.js does not activate the standard input stream. The first thing we'll do is enable standard input and echo back what we read.
20
20
21
-
Create a new file called `shell.js` and add the following:
21
+
Create a new file called `shell.js`in a brand new directory and add the following:
22
22
23
23
{% highlight javascript %}
24
24
var stdin = process.openStdin();
@@ -28,7 +28,7 @@ stdin.on('data', function (input) {
28
28
});
29
29
{% endhighlight %}
30
30
31
-
Before we go any further, let's experiment with what the above code does. Run the file:
31
+
Before we go any further, let's experiment with what the above code does. To run the file:
32
32
33
33
{% highlight bash %}
34
34
$ node shell.js
@@ -47,7 +47,7 @@ bar
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.
49
49
50
-
It's worth noting that, at this point, the buffer exists completely outside of the JavaScript memory space (the `Buffer` is an object from the C++ Node.js internals). Interacting with this buffer will move data across the native to JavaScript boundary. For example, calling `input.toString()` will create a new JavaScript string containing the entire contents of the `Buffer`. An optional encoding can be specified as the first argument of the `toString` function (for example, `input.toString('utf8')`).
50
+
It's worth noting that, at this point, the buffer exists completely outside of the JavaScript memory space (`Buffer` is an object from the C++ Node.js internals). Interacting with this buffer will move data from between the native (C++) and JavaScript boundary. For example, calling `input.toString()` will create a new JavaScript string containing the entire contents of the `Buffer`. An optional encoding can be specified as the first argument of the `toString` function (for example, `input.toString('utf8')`).
51
51
52
52
Since we're working with relatively short strings, let's go ahead and call `input.toString()` on the Buffer. Here's what your program should look like now:
53
53
@@ -61,7 +61,7 @@ stdin.on('data', function (input) {
61
61
62
62
Now starting up the shell and typing any value will result in the expected output ending with the new line character.
63
63
64
-
The next step is to parse the input string. The commands in our simple shell will take the form:
64
+
The next step is to trim and parse the input string. The commands in our simple shell will take the form:
65
65
66
66
{% highlight bash %}
67
67
command [args...]
@@ -128,7 +128,7 @@ To clarify what's happening above, here's sample output of executing the regular
128
128
129
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/).
130
130
131
-
Now, jump back to your terminal and give our shell a try!
131
+
Now, jump back to your terminal and give your shell a try!
132
132
133
133
Start up the shell with the `node` command and execute our one and only command:
134
134
@@ -219,9 +219,11 @@ commands['ls'] = function (args) {
219
219
};
220
220
{% endhighlight %}
221
221
222
+
In this example we're defining a container object `commands` and assigning anonymous functions to properties on that object. The major difference from before is that the function definitions are not part of the object initializer. There's no difference in behavior between the two approaches.
223
+
222
224
## Interacting with HTTP: Downloading a File
223
225
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.
226
+
Similarly to the `fs` module, Node.js core contains a `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.
225
227
226
228
Import the `http` module up top next to where you imported `fs`:
Copy file name to clipboardExpand all lines: _posts/2012-11-6-npm.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -117,8 +117,7 @@ Modules can be installed using the Node.js package manager `npm` to be included
117
117
For the next example, let's take a list of numbers and sort and filter them. To do this, we'll include the [underscore.js module](https://npmjs.org/package/underscore). First we need to install underscore by executing the following command from a brand new directory.
0 commit comments