Permalink
Browse files

No longer gitignoring html

  • Loading branch information...
1 parent e3c07a8 commit 2a420a75da1e2938164428bd3e30218980c6324c @tj tj committed Aug 31, 2010
View
@@ -1,4 +1 @@
.DS_Store
-build
-*.html
-book.*
View
@@ -41,7 +41,6 @@ view: book.pdf
clean:
rm -f book.*
- rm -f pages/*.html
rm -f chapters/*.html
.PHONY: view clean
View
767 book.html

Large diffs are not rendered by default.

Oops, something went wrong.
View
BIN book.pdf
Binary file not shown.
View
@@ -0,0 +1,72 @@
+<div class='mp'>
+<h1>Buffers</h1>
+<p> To handle binary data, node provides us the <code>Buffer</code> object. Buffer instances represent memory allocated independently to that of V8's heap. There are several ways to constructor a <code>Buffer</code> instance, and many ways you can manipulate it's data.</p>
+
+<p>The simplest way to construct a <code>Buffer</code> from a string is to simply pass a string as the first argument. As you can see by the log output, we now have a buffer object containing 5 bytes of data represented in hexadecimal.</p>
+
+<pre><code>var hello = new Buffer('Hello');
+
+console.log(hello);
+// =&gt; &lt;Buffer 48 65 6c 6c 6f>
+
+console.log(hello.toString());
+// =&gt; "Hello"
+</code></pre>
+
+<p>By default the encoding is "utf8", however this can be specified by passing as string as the second argument. The ellipsis below for example will be printed to stdout as the '&amp;' character when in "ascii" encoding.</p>
+
+<pre><code>var buf = new Buffer('…');
+console.log(buf.toString());
+// =&gt;
+
+var buf = new Buffer('…', 'ascii');
+console.log(buf.toString());
+// =&gt; &amp;
+</code></pre>
+
+<p>An alternative method is to pass an array of integers representing the octet stream, however in this case functionality equivalent.</p>
+
+<pre><code>var hello = new Buffer([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
+</code></pre>
+
+<p>Buffers can also be created with an integer representing the number of bytes allocated, after which we may call the <code>write()</code> method, providing an optional offset and encoding. As shown below we provide the offset of 2 bytes to our second call to <code>write()</code>, buffering "Hel", and then we continue on to write another two bytes with an offset of 3, completing "Hello".</p>
+
+<pre><code>var buf = new Buffer(5);
+buf.write('He');
+buf.write('l', 2);
+buf.write('lo', 3);
+console.log(buf.toString());
+// =&gt; "Hello"
+</code></pre>
+
+<p>The <code>.length</code> property of a buffer instance contains the byte length of the stream, compared to JavaScript strings which will simply return the number of characters. For example the ellipsis character '…' consists of three bytes, however the buffer will respond with the byte length, and not the character length.</p>
+
+<pre><code>var ellipsis = new Buffer('…', 'utf8');
+
+console.log('… string length: %d', '…'.length);
+// =&gt; … string length: 1
+
+console.log('… byte length: %d', ellipsis.length);
+// =&gt; … byte length: 3
+
+console.log(ellipsis);
+// =&gt; &lt;Buffer e2 80 a6>
+</code></pre>
+
+<p>When working with JavaScript strings, we can also pass a string through the <code>Buffer.byteLength()</code> method to determine it's byte length.</p>
+
+<p>The api is written in such a way that it is Array-like, so for example we can work with "slices" of a <code>Buffer</code> by passing offsets to the <code>slice()</code> method:</p>
+
+<pre><code>var chunk = buf.slice(4, 9);
+console.log(chunk.toString());
+// =&gt; "some"
+</code></pre>
+
+<p>Alternatively when expecting a string we can pass offsets to <code>Buffer#toString()</code>:</p>
+
+<pre><code>var buf = new Buffer('just some data');
+console.log(buf.toString('ascii', 4, 9));
+// =&gt; "some"
+</code></pre>
+
+</div>
View
@@ -0,0 +1,5 @@
+<div class='mp'>
+<h1>Connect</h1>
+<p>Connect is a ...</p>
+
+</div>
View
@@ -0,0 +1,5 @@
+<div class='mp'>
+<h1>Deployment</h1>
+<p> ...</p>
+
+</div>
View
@@ -0,0 +1,75 @@
+<div class='mp'>
+<h1>Events</h1>
+<p> Events are crucial to node, and used greatly throughout core and 3rd-party modules. Node's core library <em>events</em> supplies us with a single constructor, <em>EventEmitter</em>.</p>
+
+<h2 id="Emitting-Events">Emitting Events</h2>
+
+<p>Typically an object inherits from <em>EventEmitter</em>, however our small example below illustrates the api. First we create an <code>emitter</code>, after which we can define any number of callbacks using the <code>emitter.on()</code> method which accepts the <em>name</em> of the event, and arbitrary objects passed as data. When <code>emitter.emit()</code> is called we are only required to pass the event <em>name</em>, followed by any number of arguments, in this case the <code>first</code> and <code>last</code> name strings.</p>
+
+<pre><code>var EventEmitter = require('events').EventEmitter;
+
+var emitter = new EventEmitter;
+
+emitter.on('name', function(first, last){
+ console.log(first + ', ' + last);
+});
+
+emitter.emit('name', 'tj', 'holowaychuk');
+emitter.emit('name', 'simon', 'holowaychuk');
+</code></pre>
+
+<h2 id="Inheriting-From-EventEmitter">Inheriting From EventEmitter</h2>
+
+<p>A perhaps more practical use of <code>EventEmitter</code>, and commonly used throughout node is to inherit from it. This means we can leave <code>EventEmitter</code>'s prototype untouched, while utilizing it's api for our own means of world domination!</p>
+
+<p>To do so we begin by defining the <code>Dog</code> constructor, which of course will bark from time to time, also known as an <em>event</em>. Our <code>Dog</code> constructor accepts a <code>name</code>, followed by <code>EventEmitter.call(this)</code>, which invokes the <code>EventEmitter</code> function in context to the given argument. Doing this is essentially the same as a "super" or "parent" call in languages that support classes. This is a crucial step, as it allows <code>EventEmitter</code> to set up the <code>_events</code> property which it utilizes internally to manage callbacks.</p>
+
+<pre><code>var EventEmitter = require('events').EventEmitter;
+
+function Dog(name) {
+ this.name = name;
+ EventEmitter.call(this);
+}
+</code></pre>
+
+<p>Here we inherit from <code>EventEmitter</code>, so that we may use the methods provided such as <code>EventEmitter#on()</code> and <code>EventEmitter#emit()</code>. If the <code>__proto__</code> property is throwing you off, no worries! we will be touching on this later.</p>
+
+<pre><code>Dog.prototype.__proto__ = EventEmitter.prototype;
+</code></pre>
+
+<p>Now that we have our <code>Dog</code> set up, we can create .... simon! When simon barks we can let <em>stdout</em> know by calling <code>console.log()</code> within the callback. The callback it-self is called in context to the object, aka <code>this</code>.</p>
+
+<pre><code>var simon = new Dog('simon');
+
+simon.on('bark', function(){
+ console.log(this.name + ' barked');
+});
+</code></pre>
+
+<p>Bark twice a second:</p>
+
+<pre><code>setInterval(function(){
+ simon.emit('bark');
+}, 500);
+</code></pre>
+
+<h2 id="Removing-Event-Listeners">Removing Event Listeners</h2>
+
+<p>As we have seen event listeners are simply functions which are called when we <code>emit()</code> an event. Although not seen often we can remove these listeners by calling the <code>removeListener(type, callback)</code> method. In the example below we emit the <em>message</em> "foo bar" every <code>300</code> milliseconds, which has the callback of <code>console.log()</code>. After 1000 milliseconds we call <code>removeListener()</code> with the same arguments that we passed to <code>on()</code> originally. To compliment this method is <code>removeAllListeners(type)</code> which removes all listeners associated to the given <em>type</em>.</p>
+
+<pre><code>var EventEmitter = require('events').EventEmitter;
+
+var emitter = new EventEmitter;
+
+emitter.on('message', console.log);
+
+setInterval(function(){
+ emitter.emit('message', 'foo bar');
+}, 300);
+
+setTimeout(function(){
+ emitter.removeListener('message', console.log);
+}, 1000);
+</code></pre>
+
+</div>
View
@@ -0,0 +1,5 @@
+<div class='mp'>
+<h1>Express</h1>
+<p>Express is a ...</p>
+
+</div>
View
@@ -0,0 +1,5 @@
+<div class='mp'>
+<h1>File System</h1>
+<p> ...</p>
+
+</div>
View
@@ -0,0 +1,184 @@
+<div class='mp'>
+<h1>Globals</h1>
+<p> As we have learnt node's module system discourages the use of globals, however node provides a few important globals for use to utilize. The first and most important is the <code>process</code> global which exposes process manipulation such as signalling, exiting, the process id (pid), and more. Other globals help drive to be similar to other familiar JavaScript environments such as the browser, by providing a <code>console</code> object.</p>
+
+<h2 id="console">console</h2>
+
+<p>The <code>console</code> object contains several methods which are used to output information to <em>stdout</em> or <em>stderr</em>. Lets take a look at what each method does.</p>
+
+<h3 id="console-log-">console.log()</h3>
+
+<p>The most frequently used console method is <code>console.log()</code> simply writing to <em>stdout</em> with a line feed (<code>\n</code>). Currently aliased as <code>console.info()</code>.</p>
+
+<pre><code>console.log('wahoo');
+// =&gt; wahoo
+
+console.log({ foo: 'bar' });
+// =&gt; [object Object]
+</code></pre>
+
+<h3 id="console-error-">console.error()</h3>
+
+<p>Identical to <code>console.log()</code>, however writes to <em>stderr</em>. Aliased as <code>console.warn()</code> as well.</p>
+
+<pre><code>console.error('database connection failed');
+</code></pre>
+
+<h3 id="console-dir-">console.dir()</h3>
+
+<p>Utilizes the <em>sys</em> module's <code>inspect()</code> method to pretty-print the object to
+<em>stdout</em>.</p>
+
+<pre><code>console.dir({ foo: 'bar' });
+// =&gt; { foo: 'bar' }
+</code></pre>
+
+<h3 id="console-assert-">console.assert()</h3>
+
+<p>Asserts that the given expression is truthy, or throws an exception.</p>
+
+<pre><code>console.assert(connected, 'Database connection failed');
+</code></pre>
+
+<h2 id="process">process</h2>
+
+<p>The <code>process</code> object is plastered with goodies, first we will take a look
+at some properties that provide information about the node process itself.</p>
+
+<h3 id="process-version">process.version</h3>
+
+<p>The version property contains the node version string, for example "v0.1.103".</p>
+
+<h3 id="process-installPrefix">process.installPrefix</h3>
+
+<p>Exposes the installation prefix, in my case "<em>/usr/local</em>", as node's binary was installed to "<em>/usr/local/bin/node</em>".</p>
+
+<h3 id="process-execPath">process.execPath</h3>
+
+<p>Path to the executable itself "<em>/usr/local/bin/node</em>".</p>
+
+<h3 id="process-platform">process.platform</h3>
+
+<p>Exposes a string indicating the platform you are running on, for example "darwin".</p>
+
+<h3 id="process-pid">process.pid</h3>
+
+<p>The process id.</p>
+
+<h3 id="process-cwd-">process.cwd()</h3>
+
+<p>Returns the current working directory, for example:</p>
+
+<pre><code>cd ~ &amp;&amp; node
+node&gt; process.cwd()
+"/Users/tj"
+</code></pre>
+
+<h3 id="process-chdir-">process.chdir()</h3>
+
+<p>Changes the current working directory to the path passed.</p>
+
+<pre><code>process.chdir('/foo');
+</code></pre>
+
+<h3 id="process-getuid-">process.getuid()</h3>
+
+<p>Returns the numerical user id of the running process.</p>
+
+<h3 id="process-setuid-">process.setuid()</h3>
+
+<p>Sets the effective user id for the running process. This method accepts both a numerical id, as well as a string. For example both <code>process.setuid(501)</code>, and <code>process.setuid('tj')</code> are valid.</p>
+
+<h3 id="process-getgid-">process.getgid()</h3>
+
+<p>Returns the numerical group id of the running process.</p>
+
+<h3 id="process-setgid-">process.setgid()</h3>
+
+<p>Similar to <code>process.setuid()</code> however operates on the group, also accepting a numerical value or string representation. For example <code>process.setgid(20)</code> or <code>process.setgid('www')</code>.</p>
+
+<h3 id="process-env">process.env</h3>
+
+<p>An object containing the user's environment variables, for example:</p>
+
+<pre><code>{ PATH: '/Users/tj/.gem/ruby/1.8/bin:/Users/tj/.nvm/current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin'
+, PWD: '/Users/tj/ebooks/masteringnode'
+, EDITOR: 'mate'
+, LANG: 'en_CA.UTF-8'
+, SHLVL: '1'
+, HOME: '/Users/tj'
+, LOGNAME: 'tj'
+, DISPLAY: '/tmp/launch-YCkT03/org.x:0'
+, _: '/usr/local/bin/node'
+, OLDPWD: '/Users/tj'
+}
+</code></pre>
+
+<h3 id="process-argv">process.argv</h3>
+
+<p>When executing a file with the <code>node</code> executable <code>process.argv</code> provides access to the argument vector, the first value being the node executable, second being the filename, and remaining values being the arguments passed.</p>
+
+<p>For example our source file <em>./src/process/misc.js</em> can be executed by running:</p>
+
+<pre><code>$ node src/process/misc.js foo bar baz
+</code></pre>
+
+<p>in which we call <code>console.dir(process.argv)</code>, outputting the following:</p>
+
+<pre><code>[ 'node'
+, '/Users/tj/EBooks/masteringnode/src/process/misc.js'
+, 'foo'
+, 'bar'
+, 'baz'
+]
+</code></pre>
+
+<h3 id="process-exit-">process.exit()</h3>
+
+<p>The <code>process.exit()</code> method is synonymous with the C function <code>exit()</code>, in which a exit code > 0 is passed indicating failure, or 0 to indicate success. When invoked the <em>exit</em> event is emitted, allowing a short time for arbitrary processing to occur before <code>process.reallyExit()</code> is called with the given status code.</p>
+
+<h3 id="process-on-">process.on()</h3>
+
+<p>The process itself is an <code>EventEmitter</code>, allowing you to do things like listen for uncaught exceptions, via the <em>uncaughtException</em> event:</p>
+
+<pre><code>process.on('uncaughtException', function(err){
+ console.log('got an error: %s', err.message);
+ process.exit(1);
+});
+
+setTimeout(function(){
+ throw new Error('fail');
+}, 100);
+</code></pre>
+
+<h3 id="process-kill-">process.kill()</h3>
+
+<p><code>process.kill()</code> method sends the signal passed to the given <em>pid</em>, defaulting to <strong>SIGINT</strong>. In our example below we send the <strong>SIGTERM</strong> signal to the same node process to illustrate signal trapping, after which we output "terminating" and exit. Note that our second timeout of 1000 milliseconds is never reached.</p>
+
+<pre><code>process.on('SIGTERM', function(){
+ console.log('terminating');
+ process.exit(1);
+});
+
+setTimeout(function(){
+ console.log('sending SIGTERM to process %d', process.pid);
+ process.kill(process.pid, 'SIGTERM');
+}, 500);
+
+setTimeout(function(){
+ console.log('never called');
+}, 1000);
+</code></pre>
+
+<h3 id="errno">errno</h3>
+
+<p>The <code>process</code> object is host of the error numbers, these reference what you would find in C-land, for example <code>process.EPERM</code> represents a permission based error, while <code>process.ENOENT</code> represents a missing file or directory. Typically these are used within bindings to bridge the gap between c++ and JavaScript, however useful for handling exceptions as well:</p>
+
+<pre><code>if (err.errno === process.ENOENT) {
+ // Display a 404 "Not Found" page
+} else {
+ // Display a 500 "Internal Server Error" page
+}
+</code></pre>
+
+</div>
View
@@ -0,0 +1,13 @@
+<div class='mp'>
+<h1>HTTP</h1>
+<p> ...</p>
+
+<h2 id="HTTP-Servers">HTTP Servers</h2>
+
+<p> ...</p>
+
+<h2 id="HTTP-Clients">HTTP Clients</h2>
+
+<p> ...</p>
+
+</div>
Oops, something went wrong.

0 comments on commit 2a420a7

Please sign in to comment.