Skip to content
This repository
Browse code

Update the welcome article to node v0.1.102.

  • Loading branch information...
commit 64b1eab56405c5ed19c967a4216c127d08206711 1 parent 4172c1e
Tim Caswell authored July 27, 2010
39  articles/welcome.markdown
Source Rendered
... ...
@@ -1,7 +1,7 @@
1 1
 Title: Welcome to HowToNode.org
2 2
 Author: Tim Caswell
3 3
 Date: Tue Feb 02 2010 10:16:51 GMT-0600 (CST)
4  
-Node: v0.1.91
  4
+Node: v0.1.102
5 5
 
6 6
 **How To Node** is a blog featuring projects and tutorials relating to the Node.js project.
7 7
 
@@ -9,6 +9,8 @@ This article will describe how the blog works.  Teach a few node concepts and de
9 9
 
10 10
 ## Sample App - The node-blog engine. ##
11 11
 
  12
+**UPDATE** - This site now runs on another engine, but this article is still good as a general introduction to node.
  13
+
12 14
 This entire site is hosted by an nginx server as static html files.  That's right, you can hit refresh as many times as you want and my node code won't know about it.  Think about it as super page caching.
13 15
 
14 16
 So where does node come in?  Well there is a small node app running on a high port listening for POST hooks from github.  Every time someone pushes changes to the repository that represents the source of this site, the running node app will regenerate all the static files.
@@ -29,28 +31,16 @@ First we need a running http server to listen to POST hooks from [github][].  Th
29 31
 
30 32
 We'll start with a small HTTP server.  Almost straight from the docs.
31 33
 
32  
-    var sys = require('sys'),
33  
-       http = require('http');
34  
-
35  
-    http.createServer(function (req, res) {
36  
-      setTimeout(function () {
37  
-        res.writeHead(200, {'Content-Type': 'text/plain'});
38  
-        res.write('Hello World');
39  
-        res.end();
40  
-      }, 2000);
41  
-    }).listen(8000);
42  
-
43  
-    sys.puts('Server running at http://127.0.0.1:8000/');
  34
+<welcome/server.js>
44 35
 
45  
-First we're loading a couple of external libraries.  Node is partially compatible with the CommonJS securable module system.  This means that library developers can write reusable JavaScript code and you can `require` it into your project.  The two we're loading here are [sys][] and [http][]. Loading `sys` allows us to do several low-level operations like write to the terminal and execute shell commands.  Loading `http` allows us to start a standalone HTTP server.
  36
+First we're loading a couple of external libraries.  Node is partially compatible with the CommonJS securable module system.  This means that library developers can write reusable JavaScript code and you can `require` it into your project.  Loading `http` allows us to start a standalone HTTP server.
46 37
 
47 38
 Bascically, this is the order of operations:
48 39
 
49  
- - `sys` is loaded and all execution stops until it's done
50  
- - Same for the `http` library.  `require` is about the only blocking method in node.
  40
+ - `http` is loaded and all execution stops until it's done.  `require` is about the only blocking function in node.
51 41
  - Then http.createServer is called and a function is passed to it.  It returns immediately
52 42
  - Listen is called on the resultant server and returns immediately.
53  
- - sys.puts is called.  This is also async, so we don't wait for the text to actually appear on the console.
  43
+ - `console.log` is called.  This is also async, so we don't wait for the text to actually appear on the console.
54 44
 
55 45
 Now what happens next starts to get tricky.  Node runs in a single thread of execution, but as soon as a block of synchronous code is done, the event loop runs the next event in the queue.  So what happens next depends on which IO was ready first.  Practically `listen` and `puts` both happen pretty quickly, so it doesn't usually matter. Se we'll assume that a message got printed to the console and an HTTP server is running port 8000.
56 46
 
@@ -58,19 +48,7 @@ Whenever the server gets a web request, the function we passed in to createServe
58 48
 
59 49
 So to convert this to a Github POST hook, we'll keep it simple and assume all the logic is in another module.
60 50
 
61  
-    var sys = require('sys'),
62  
-       http = require('http'),
63  
-       rebuild = require('./builder').rebuild;
64  
-
65  
-    http.createServer(function (req, res) {
66  
-      rebuild(function (output) {
67  
-        res.writeHead(200, {'Content-Type': 'text/plain'});
68  
-        res.write(output);
69  
-        res.end();
70  
-      })
71  
-    }).listen(8000);
72  
-
73  
-    sys.puts('Github hook running at http://127.0.0.1:8000/');
  51
+<welcome/github-hook.js>
74 52
 
75 53
 So whenever a request is received, we call the external `rebuild` method.  The reason we pass in a callback instead of getting the return value is because the builder will need to do some of it's own IO and can't return a meaningful response right away.  When it's done, we'll be notified and then pass on the output to the browser.
76 54
 
@@ -86,7 +64,6 @@ Add an author entry for yourself and write your article.  When done send any of
86 64
 
87 65
 [creationix]: http://github.com/creationix
88 66
 [miksago]: http://github.com/miksago
89  
-[sys]: http://nodejs.org/api.html#_system_module
90 67
 [http]: http://nodejs.org/api.html#_http
91 68
 [node]: http://nodejs.org/
92 69
 [github]: http://github.com/
11  articles/welcome/github-hook.js
... ...
@@ -0,0 +1,11 @@
  1
+var http = require('http'),
  2
+    rebuild = require('./builder').rebuild;
  3
+
  4
+http.createServer(function (req, res) {
  5
+  rebuild(function (output) {
  6
+    res.writeHead(200, {'Content-Type': 'text/plain'});
  7
+    res.end(output);
  8
+  })
  9
+}).listen(8000);
  10
+
  11
+console.log('Github hook running at http://127.0.0.1:8000/');
10  articles/welcome/server.js
... ...
@@ -0,0 +1,10 @@
  1
+var http = require('http');
  2
+
  3
+http.createServer(function (req, res) {
  4
+  setTimeout(function () {
  5
+    res.writeHead(200, {'Content-Type': 'text/plain'});
  6
+    res.end('Hello World');
  7
+  }, 2000);
  8
+}).listen(8000);
  9
+
  10
+console.log('Server running at http://127.0.0.1:8000/');

0 notes on commit 64b1eab

Please sign in to comment.
Something went wrong with that request. Please try again.