Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update hello-node article.

  • Loading branch information...
commit 42a499fee219e69604f6ec24f60fc3bcce6d250c 1 parent 43ea54d
Tim Caswell creationix authored
3  .gitmodules
@@ -4,3 +4,6 @@
4 4 [submodule "articles/step-of-conductor/conductor"]
5 5 path = articles/step-of-conductor/conductor
6 6 url = git://github.com/creationix/conductor.git
  7 +[submodule "articles/hello-node/node-router"]
  8 + path = articles/hello-node/node-router
  9 + url = git://github.com/creationix/node-router.git
60 articles/hello-node.markdown
Source Rendered
... ... @@ -1,7 +1,7 @@
1 1 Title: Hello Node!
2 2 Author: Tim Caswell
3 3 Date: Mon Mar 22 2010 10:55:41 GMT-0500 (CDT)
4   -NodeVersion: v0.1.33-143-gaa0f994
  4 +Node: v0.1.90
5 5
6 6 In programming literature it has become the standard to create a hello world program as the first example. This article will go through a few simple hello world type examples with everything from simple terminal output to an http server that uses an external framework for some semantic sugar.
7 7
@@ -15,11 +15,7 @@ Here are four simple hello world examples. The comments in the code explain how
15 15
16 16 This example is about as plain as it can get. It prints the words "Hello World" to the terminal.
17 17
18   - // Load the sys module since we want to write to the terminal
19   - var sys = require('sys');
20   -
21   - // Call the puts function on the sys module.
22   - sys.puts("Hello World");
  18 +<hello-node/hello-console.js*>
23 19
24 20 You can run this by putting it in a file called "hello-console.js" and running it with `node hello-console.js`
25 21
@@ -27,65 +23,19 @@ You can run this by putting it in a file called "hello-console.js" and running i
27 23
28 24 I'd guess that while it's not the only use case for node.JS, most people are using it as a web application platform. So the next example will be a simple HTTP server that responds to every request with the plain text message "Hello World"
29 25
30   - // Load the sys module for console writing.
31   - var sys = require('sys');
32   - // Load the http module to create an http server.
33   - var http = require('http');
34   -
35   - // Configure our HTTP server to respond with Hello World to all requests.
36   - var server = http.createServer(function (request, response) {
37   - response.writeHead(200, {"Content-Type": "text/plain"});
38   - response.write("Hello World\n");
39   - response.close();
40   - });
41   -
42   - // Listen on port 8000, IP defaults to 127.0.0.1
43   - server.listen(8000);
44   -
45   - // Put a friendly message on the terminal
46   - sys.puts("Server running at http://127.0.0.1:8000/");
  26 +<hello-node/hello-http.js>
47 27
48 28 ## Hello TCP
49 29
50 30 Node also makes an excellent TCP server, and here is an example that responds to all TCP connections with the message "Hello World" and then closes the connection.
51 31
52   - // Load the sys module for console writing.
53   - var sys = require('sys');
54   - // Load the net module to create a tcp server.
55   - var net = require('net');
56   -
57   - // Setup a tcp server
58   - var server = net.createServer(function (socket) {
59   -
60   - // Every time someone connects, greet and then close the connection.
61   - socket.addListener("connect", function () {
62   - sys.puts("Connection from " + socket.remoteAddress);
63   - socket.write("Hello World\n");
64   - socket.close();
65   - });
66   -
67   - });
68   -
69   - // Fire up the server bound to port 7000 on localhost
70   - server.listen(7000, "localhost");
71   -
72   - // Put a friendly message on the terminal
73   - sys.puts("TCP server listening on port 7000 at localhost.");
  32 +<hello-node/hello-tcp.js>
74 33
75 34 ## Hello Router
76 35
77 36 Often you won't be using the node built-in libraries because they are designed to be very low level. This makes node quick, nimble, and easy to maintain, but is usually not enough to get started on a real world application. My first node framework is `node-router`. This example shows an HTTP server that responds with "Hello World" to all requests to "/" and responds with a 404 error to everything else.
78 37
79   - // Load the node-router library by creationix
80   - var server = require('node-router').getServer();
81   -
82   - // Configure our HTTP server to respond with Hello World the root request
83   - server.get("/", function (request, response) {
84   - response.simpleText(200, "Hello World!");
85   - });
86   -
87   - // Listen on port 8080 on localhost
88   - server.listen(8080, "localhost");
  38 +<hello-node/hello-router.js>
89 39
90 40 In order to test this, you will need to install the `node-router` library. There are two ways to do this. You can either install it into a path that node recognizes (I create a symlink into ~/.node_libraries) or put the `node-router.js` file in your application and reference it locally. See the [node docs on modules][] for more details on how modules work.
91 41
2  articles/hello-node/hello-router.js
@@ -7,4 +7,4 @@ server.get("/", function (request, response) {
7 7 });
8 8
9 9 // Listen on port 8080 on localhost
10   -server.listen(8080, "localhost");
  10 +server.listen(8000, "localhost");
2  articles/hello-node/hello-tcp.js
@@ -10,7 +10,7 @@ var server = net.createServer(function (socket) {
10 10 socket.addListener("connect", function () {
11 11 sys.puts("Connection from " + socket.remoteAddress);
12 12 socket.write("Hello World\n");
13   - socket.close();
  13 + socket.end();
14 14 });
15 15
16 16 });
1  articles/hello-node/node-router
... ... @@ -0,0 +1 @@
  1 +Subproject commit cab031f83527e1ea0e863eee00daf52866f8523c

0 comments on commit 42a499f

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