Skip to content
This repository
Newer
Older
100644 420 lines (305 sloc) 14.317 kb
2db90096 »
2011-03-30 Proper titles
1 % Felix's Node.js Beginners Guide
ad45db14 »
2011-03-29 Initial import of iPad Notes
2
b22fdbb4 »
2011-03-30 80 character line length
3 There is lots of information about node.js, but given the rapid pace at which it
90b0b875 »
2011-04-06 Simple spelling and phrasing changes
4 is developing, it can be difficult for beginners to find good, current
5 information on how to get started. This guide aims to provide exactly that,
6 whilst staying updated with the latest stable version of node.js.
ad45db14 »
2011-03-29 Initial import of iPad Notes
7
b22fdbb4 »
2011-03-30 80 character line length
8 This guide has been updated to reflect the latest changes in node 0.4.x, the
9 currently stable branch of node.js.
ad45db14 »
2011-03-29 Initial import of iPad Notes
10
11 ## Learning JavaScript
12
b22fdbb4 »
2011-03-30 80 character line length
13 This guide assumes that you are already familar with JavaScript. If you are not,
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
14 you should go ahead and read: [Eloquent JavaScript][eloquentjs], a free online
15 book by [Marijn Haverbeke][marijn].
16
17 [eloquentjs]: http://eloquentjavascript.net/
18 [marijn]: http://twitter.com/marijnjh
ad45db14 »
2011-03-29 Initial import of iPad Notes
19
20 ## Hello World Tutorial
21
b22fdbb4 »
2011-03-30 80 character line length
22 This tutorial guides you through installing node.js, including the creation of a
23 simple hello world http server.
ad45db14 »
2011-03-29 Initial import of iPad Notes
24
25 ### Installation
26
b22fdbb4 »
2011-03-30 80 character line length
27 First of all: You should run a *nix operating system in order to use node.js at
28 this point. Linux and OSX are recommended, but you should also be able to use
29 FreeBSD and cygwin (on windows). A full windows port of node.js is in the works,
90b0b875 »
2011-04-06 Simple spelling and phrasing changes
30 but it is not yet ready for public usage.
ad45db14 »
2011-03-29 Initial import of iPad Notes
31
b22fdbb4 »
2011-03-30 80 character line length
32 The most common way to install node.js is to directly compile it from the
33 downloaded source code. There are also various packages available for different
34 package managers, but since their update frequency varies, I recommend
35 installing from source.
ad45db14 »
2011-03-29 Initial import of iPad Notes
36
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
37 You can get the latest source code from [nodejs.org][nodejs]. Use the commands
38 below to download and install v0.4.4:
39
40 [nodejs]: http://nodejs.org/
ad45db14 »
2011-03-29 Initial import of iPad Notes
41
baf44f0d »
2011-03-29 Indicate code type for beginner guide
42 ~~~ {.shell}
0f8b5358 »
2011-04-01 Less magic for the install instructions
43 $ wget http://nodejs.org/dist/node-v0.4.4.tar.gz
44 $ tar -xzf node-v0.4.4.tar.gz
45 $ cd node-v0.4.4.tar.gz
baf44f0d »
2011-03-29 Indicate code type for beginner guide
46 $ ./configure
47 $ sudo make install
48 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
49
b22fdbb4 »
2011-03-30 80 character line length
50 Node.js itself has no external dependencies except common build tools as well as
51 pythons for the build system itself. On OSX you must install XCode for this to
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
52 work, and on Ubuntu you probably have to run:
53
54 ~~~ {.shell}
55 $ apt-get -y install build-essential
56 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
57
58 ### The interactive node.js shell
59
b22fdbb4 »
2011-03-30 80 character line length
60 If everything worked, you should be able to invoke the interactive node.js shell
61 like this:
ad45db14 »
2011-03-29 Initial import of iPad Notes
62
baf44f0d »
2011-03-29 Indicate code type for beginner guide
63 ~~~ {.shell}
64 $ node
ad45db14 »
2011-03-29 Initial import of iPad Notes
65 > console.log('Hello World');
66 Hello World
baf44f0d »
2011-03-29 Indicate code type for beginner guide
67 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
68
82a1d292 »
2011-03-30 Tyypos
69 The interactive shell (also called [REPL][]) is a great place to test simple
da323743 »
2011-03-30 Spellcheck
70 one liners, and can also be directly [embedded][repldocs] into your node.js
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
71 applications. In order to get out of it, simply press Ctrl + C.
ad45db14 »
2011-03-29 Initial import of iPad Notes
72
b22fdbb4 »
2011-03-30 80 character line length
73 The REPL also comes with many other great features, most importantly tab
74 auto-completion.
ad45db14 »
2011-03-29 Initial import of iPad Notes
75
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
76 [repldocs]: http://nodejs.org/docs/v0.4.4/api/repl.html
82a1d292 »
2011-03-30 Tyypos
77 [REPL]: http://en.wikipedia.org/wiki/REPL
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
78
ad45db14 »
2011-03-29 Initial import of iPad Notes
79 ### Your first program
80
b22fdbb4 »
2011-03-30 80 character line length
81 Writing a node.js program is as simple as creating a new file with a '.js'
82 extension. For example you could create a simple 'hello_world.js' file with the
83 following content:
ad45db14 »
2011-03-29 Initial import of iPad Notes
84
baf44f0d »
2011-03-29 Indicate code type for beginner guide
85 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
86 console.log('Hello World');
baf44f0d »
2011-03-29 Indicate code type for beginner guide
87 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
88
89 After you have saved the file, you can execute it from your terminal like so:
90
baf44f0d »
2011-03-29 Indicate code type for beginner guide
91 ~~~ {.shell}
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
92 $ node hello.js
ad45db14 »
2011-03-29 Initial import of iPad Notes
93 Hello World
baf44f0d »
2011-03-29 Indicate code type for beginner guide
94 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
95
96 ### A hello world http server
97
b22fdbb4 »
2011-03-30 80 character line length
98 Now printing hello world to a terminal isn't all that exciting. Let's take the
99 next step and write a program that responds to hello world via http. We'll call
100 the file 'hello_http.js' and put the following code into it:
ad45db14 »
2011-03-29 Initial import of iPad Notes
101
baf44f0d »
2011-03-29 Indicate code type for beginner guide
102 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
103 var http = require('http');
104
105 var server = http.createServer(function(req, res) {
c91072e9 »
2011-03-30 Fix: res.sendHead is wrong
106   res.writeHead(200);
ad45db14 »
2011-03-29 Initial import of iPad Notes
107   res.end('Hello Http');
108 });
109 server.listen(8080);
baf44f0d »
2011-03-29 Indicate code type for beginner guide
110 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
111
112 Now lets run this program from the terminal by typing:
113
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
114 ~~~ {.shell}
115 $ node hello_http.js
116 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
117
b22fdbb4 »
2011-03-30 80 character line length
118 The first thing you'll notice is that this program, unlike our first one,
119 doesn't exit right away. That's because a node program will always run until
90b0b875 »
2011-04-06 Simple spelling and phrasing changes
120 it's certain that no further events are possible. In this case the open http
121 server is the source of events that will keep things going.
ad45db14 »
2011-03-29 Initial import of iPad Notes
122
b22fdbb4 »
2011-03-30 80 character line length
123 Testing the server is as simple as opening a new browser tab, and navigating to
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
124 the following url: [http://localhost:8080/](http://localhost:8080/). As
125 expected, you should see a response that reads: 'Hello Http'.
ad45db14 »
2011-03-29 Initial import of iPad Notes
126
b22fdbb4 »
2011-03-30 80 character line length
127 Alternatively, you could also open up a new terminal and use curl to test your
128 server:
ad45db14 »
2011-03-29 Initial import of iPad Notes
129
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
130 ~~~ {.shell}
131 $ curl localhost:8080
132 Hello Http
133 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
134
b22fdbb4 »
2011-03-30 80 character line length
135 Now let's have a closer look at the steps involved in our little program. In the
136 first line, we include the http core module and assign it to a variable called
137 http. You will find more information on this in the next section about the
138 module system.
ad45db14 »
2011-03-29 Initial import of iPad Notes
139
b22fdbb4 »
2011-03-30 80 character line length
140 Next we create a variable called server by calling `http.createServer`. The
141 argument passed into this call is a closure that is called whenever an http
142 request comes in.
ad45db14 »
2011-03-29 Initial import of iPad Notes
143
b22fdbb4 »
2011-03-30 80 character line length
144 Finally we call `server.listen(8080)` to tell node.js the port on which we want
145 our server to run. If you want to run on port 80, your program needs to be
146 executed as root.
ad45db14 »
2011-03-29 Initial import of iPad Notes
147
b22fdbb4 »
2011-03-30 80 character line length
148 Now when you point your browser to 'localhost:8080', the connection closure is
149 invoked with a `req` and `res` object. The `req` is a readable stream that emits
150 'data' events for each incoming piece of data (like a form submission or file
151 upload). The `res` object is a writable stream that is used to send data back to
152 the client. In our case we are simply sending a 200 OK header, as well as the
153 body 'Hello Http'.
ad45db14 »
2011-03-29 Initial import of iPad Notes
154
155 ## The module system
156
b22fdbb4 »
2011-03-30 80 character line length
157 In order to structure your program into different files, node.js provides you
158 with a simple module system.
ad45db14 »
2011-03-29 Initial import of iPad Notes
159
b22fdbb4 »
2011-03-30 80 character line length
160 To illustrate the approach, let's create a new file called 'main.js' with the
161 following content:
ad45db14 »
2011-03-29 Initial import of iPad Notes
162
baf44f0d »
2011-03-29 Indicate code type for beginner guide
163 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
164 var hello = require('./hello');
165 hello.world();
baf44f0d »
2011-03-29 Indicate code type for beginner guide
166 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
167
b22fdbb4 »
2011-03-30 80 character line length
168 As you have probably guessed, the `require('./hello')` is used to import the
169 contents from another JavaScript file. The initial './' indicates that the file
e9c5b803 »
2011-08-01 what a lovely as.
170 is located in the same directory as 'main.js'. Also note that you don't have to
b22fdbb4 »
2011-03-30 80 character line length
171 provide the file extension, as '.js' is assumed by default.
ad45db14 »
2011-03-29 Initial import of iPad Notes
172
173 So let's go ahead and create our 'hello.js' file, with the following content:
174
baf44f0d »
2011-03-29 Indicate code type for beginner guide
175 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
176 exports.world = function() {
177   console.log('Hello World');
178 }
baf44f0d »
2011-03-29 Indicate code type for beginner guide
179 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
180
da323743 »
2011-03-30 Spellcheck
181 What you notice here, is that we are assigning a property called 'world' to an
b22fdbb4 »
2011-03-30 80 character line length
182 object called 'exports'. Such an 'exports' object is available in every module,
183 and it is returned whenever the `require` function is used to include the
184 module. If we now go ahead and run our 'main.js' program, we will see the
185 expected output:
ad45db14 »
2011-03-29 Initial import of iPad Notes
186
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
187 ~~~ {.shell}
188 $ node main.js
189 Hello World
190 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
191
b22fdbb4 »
2011-03-30 80 character line length
192 At this point it should also be mentioned that many node users are overwriting
193 the exports object directly like so:
ad45db14 »
2011-03-29 Initial import of iPad Notes
194
baf44f0d »
2011-03-29 Indicate code type for beginner guide
195 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
196 module.exports = function() {
197   // ...
198 }
baf44f0d »
2011-03-29 Indicate code type for beginner guide
199 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
200
b22fdbb4 »
2011-03-30 80 character line length
201 As you might have expected, this will directly cause the `require` function to
82a1d292 »
2011-03-30 Tyypos
202 return the assigned function. This is useful if you're doing [object oriented
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
203 programming][oopguide], where each file exports the constructor of one class.
ad45db14 »
2011-03-29 Initial import of iPad Notes
204
b22fdbb4 »
2011-03-30 80 character line length
205 The next thing you need to know about the module system is how it deals with
206 `require` calls that don't include a relative hint about the location of the
207 included file. Take for example:
ad45db14 »
2011-03-29 Initial import of iPad Notes
208
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
209 [oopguide]: object_oriented_programming.html
210
baf44f0d »
2011-03-29 Indicate code type for beginner guide
211 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
212 var http = require('http');
baf44f0d »
2011-03-29 Indicate code type for beginner guide
213 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
214
b22fdbb4 »
2011-03-30 80 character line length
215 What node.js will do in this case, is to first look if there is a core module
216 named http, and since that's the case, return that directly. But what about
217 non-core modules, such as 'mysql'?
ad45db14 »
2011-03-29 Initial import of iPad Notes
218
baf44f0d »
2011-03-29 Indicate code type for beginner guide
219 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
220 var mysql = require('mysql');
baf44f0d »
2011-03-29 Indicate code type for beginner guide
221 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
222
90b0b875 »
2011-04-06 Simple spelling and phrasing changes
223 In this case node.js will walk up the directory tree, moving through each
224 parent directory in turn, checking in each to see if there is a folder called
225 'node_modules'. If such a folder is found, node.js will look into this folder
226 for a file called 'mysql.js'. If no matching file is found and the directory
227 root '/' is reached, node.js will give up and throw an exception.
ad45db14 »
2011-03-29 Initial import of iPad Notes
228
b22fdbb4 »
2011-03-30 80 character line length
229 At this point node.js also considers an additional, mutable list of alternative
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
230 include directories which are accessible through the `require.paths` array.
b22fdbb4 »
2011-03-30 80 character line length
231 However, there is intense debate about removing this feature, so you are
232 probably better off ignoring it.
ad45db14 »
2011-03-29 Initial import of iPad Notes
233
82a1d292 »
2011-03-30 Tyypos
234 Last but not least, node.js also lets you create an 'index.js' file, which
b22fdbb4 »
2011-03-30 80 character line length
235 indicates the main include file for a directory. So if you call
236 `require('./foo')`, both a 'foo.js' file as well as an 'foo/index.js' file will
237 be considered, this goes for non-relative includes as well.
ad45db14 »
2011-03-29 Initial import of iPad Notes
238
239 ## Using EventEmitters
240
da323743 »
2011-03-30 Spellcheck
241 Node.js implements the [observer pattern][observerpattern] using a class called
242 EventEmitter. Whenever there is an object that represents the source of several
243 kinds of events, node.js usually makes the underlaying class inherit from
244 EventEmitter.
ad45db14 »
2011-03-29 Initial import of iPad Notes
245
82a1d292 »
2011-03-30 Tyypos
246 Using EventEmitter's is pretty straight-forward. You can listen to a specific event by
b22fdbb4 »
2011-03-30 80 character line length
247 calling the 'on()' function on your object, providing the name of the event, as
248 well as a callback closure as the parameters. For example:
ad45db14 »
2011-03-29 Initial import of iPad Notes
249
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
250 [observerpattern]: http://en.wikipedia.org/wiki/Observer_pattern
251
baf44f0d »
2011-03-29 Indicate code type for beginner guide
252 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
253 var data = '';
254 req
255   .on('data', function(chunk) {
256     data += chunk;
257   })
258   .on('end', function() {
259     console.log('POST data: %s', data);
260   })
baf44f0d »
2011-03-29 Indicate code type for beginner guide
261 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
262
b22fdbb4 »
2011-03-30 80 character line length
263 As you can see, the `on()` function also returns a reference to the object it
da323743 »
2011-03-30 Spellcheck
264 belongs to, allowing you to chain several of such event listeners.
ad45db14 »
2011-03-29 Initial import of iPad Notes
265
da323743 »
2011-03-30 Spellcheck
266 If you're only interested in the first occurrence of an event, you can use the
b22fdbb4 »
2011-03-30 80 character line length
267 `once()` function instead.
ad45db14 »
2011-03-29 Initial import of iPad Notes
268
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
269 Finally, you can remove event listeners by using the `removeListener` function.
b22fdbb4 »
2011-03-30 80 character line length
270 Please note that the argument to this function is a reference to the callback
82a1d292 »
2011-03-30 Tyypos
271 you are trying to remove, not the name of the event:
ad45db14 »
2011-03-29 Initial import of iPad Notes
272
baf44f0d »
2011-03-29 Indicate code type for beginner guide
273 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
274 var onData = function(chunk) {
275   console.log(chunk);
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
276   req.removeListener(onData);
ad45db14 »
2011-03-29 Initial import of iPad Notes
277 }
278
279 req.on('data', onData);
baf44f0d »
2011-03-29 Indicate code type for beginner guide
280 ~~~
281
ad45db14 »
2011-03-29 Initial import of iPad Notes
282 The example above is essentially identical to the `once()` function.
283
284 ## Next Steps
285
b22fdbb4 »
2011-03-30 80 character line length
286 Now that you know your node.js basics, you're probably best off by writing a few
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
287 little programs yourself. The best place to start out is [node's api
da323743 »
2011-03-30 Spellcheck
288 documentation][nodeapidocs], using it as a source of inspiration for something
289 you want to play with.
ad45db14 »
2011-03-29 Initial import of iPad Notes
290
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
291 [nodeapidocs]: http://nodejs.org/docs/v0.4.4/api/
292
ad45db14 »
2011-03-29 Initial import of iPad Notes
293 ## Debugging node.js apps
294
b22fdbb4 »
2011-03-30 80 character line length
295 There are many ways to debug your node.js based applications. Personally I
296 prefer to do as little debugging as possible, so I strictly follow the advice of
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
297 the [test driven development guide][tddguide].
ad45db14 »
2011-03-29 Initial import of iPad Notes
298
b22fdbb4 »
2011-03-30 80 character line length
299 However, if you find yourself in a situation where you need to locate a tricky
300 bug in an existing applications, here are a few approaches that can help.
ad45db14 »
2011-03-29 Initial import of iPad Notes
301
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
302 [tddguide]: test_driven_development.html
303
ad45db14 »
2011-03-29 Initial import of iPad Notes
304 ### Using console.log()
305
b22fdbb4 »
2011-03-30 80 character line length
306 The easiest way to understand a problem is by inspecting objects using
307 console.log(). You can either directly pass in objects as parameters:
ad45db14 »
2011-03-29 Initial import of iPad Notes
308
baf44f0d »
2011-03-29 Indicate code type for beginner guide
309 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
310 var foo = {bar: 'foobar'};
311 console.log(foo);
baf44f0d »
2011-03-29 Indicate code type for beginner guide
312 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
313
5be44292 »
2011-03-30 one more typo
314 Or you can use its sprintf()-like capabilities to format your debug output:
ad45db14 »
2011-03-29 Initial import of iPad Notes
315
baf44f0d »
2011-03-29 Indicate code type for beginner guide
316 ~~~ {.javascript}
ad45db14 »
2011-03-29 Initial import of iPad Notes
317 var foo = {bar: 'foobar'};
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
318 console.log('Hello %s, this is my object: %j', 'World', foo);
baf44f0d »
2011-03-29 Indicate code type for beginner guide
319 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
320
321 ### Using the node debugger
322
b22fdbb4 »
2011-03-30 80 character line length
323 If console.log() isn't your thing, or you think your problem can be better
324 analyzed using breakpoints, the node's built-in debugger is a great choice. You
6061c7d0 »
2011-03-30 Typo
325 can invoke the debugger by simply calling:
ad45db14 »
2011-03-29 Initial import of iPad Notes
326
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
327 ~~~ {.shell}
0864e311 »
2011-03-30 Typo
328 $ node.js debug my_file.js
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
329 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
330
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
331 *Work in progress, please come back later ...*
ad45db14 »
2011-03-29 Initial import of iPad Notes
332
333 ### Using the WebKit Inspector
334
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
335 *Work in progress, please come back later ...*
ad45db14 »
2011-03-29 Initial import of iPad Notes
336
337 ## Frameworks
338
b22fdbb4 »
2011-03-30 80 character line length
339 If you're new to node.js, you might not want to re-invent the wheel when it
340 comes to parsing POST requests, routing urls or rendering views. In this case,
341 you probably want to use one of the popular web frameworks. This section gives
da323743 »
2011-03-30 Spellcheck
342 you a quick overview over the popular choices, and my opinionated take on them.
ad45db14 »
2011-03-29 Initial import of iPad Notes
343
344 ### Express
345
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
346 At this point [express][express] is probably the go-to framework for most
347 node.js developers. It's relatively mature, and includes the [connect][connect]
348 (think rack) middleware layer. Included in the package are routing,
349 configuration, a template engine, POST parsing and many other features.
ad45db14 »
2011-03-29 Initial import of iPad Notes
350
da323743 »
2011-03-30 Spellcheck
351 While express is a solid framework, it's currently addressing a much smaller
b22fdbb4 »
2011-03-30 80 character line length
352 scope than fullstack frameworks like Rails, CakePHP or Django. It's more
353 comparable to Sinatra, and unfortunately doesn't really make a big effort to
5f043a64 »
2011-03-30 Typos
354 differentiate itself from its Ruby roots into something that feels natural in
b22fdbb4 »
2011-03-30 80 character line length
355 JavaScript. Anyhow, short of writing your own framework, it's certainly a great
356 choice at this point.
ad45db14 »
2011-03-29 Initial import of iPad Notes
357
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
358 [express]: http://expressjs.com/
359 [connect]: https://github.com/senchalabs/connect
ad45db14 »
2011-03-29 Initial import of iPad Notes
360
361 ### fab.js
362
b22fdbb4 »
2011-03-30 80 character line length
363 You think you know JavaScript? Think again. Originally inspired by jQuery's
da323743 »
2011-03-30 Spellcheck
364 chaining, [fab.js][fabjs] has taken a very unconventional approach of twisting
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
365 JavaScript beyond most peoples brain capacity. Each function returns another
da323743 »
2011-03-30 Spellcheck
366 function, eliminating the need for method names altogether, while giving the
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
367 resulting code a lisp-esque look & feel.
ad45db14 »
2011-03-29 Initial import of iPad Notes
368
b22fdbb4 »
2011-03-30 80 character line length
369 At this point I don't consider fab.js production-ready, but if you're still
370 exploring the world of node.js, you should absolutely try it out at least once.
371 If nothing else, fab.js shows the world that JavaScript doesn't have to copy
5f043a64 »
2011-03-30 Typos
372 Ruby, Python or PHP when it comes to web frameworks, and can go its own unique
b22fdbb4 »
2011-03-30 80 character line length
373 ways.
ad45db14 »
2011-03-29 Initial import of iPad Notes
374
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
375 [fabjs]: http://fabjs.org/
ad45db14 »
2011-03-29 Initial import of iPad Notes
376
377 ## Hosting & Deployment
378
379 ### Quick & Dirty Deployment
380
b22fdbb4 »
2011-03-30 80 character line length
381 If you have just written your first node.js application, and you want to get it
382 running as fast as possible, this is how to do it:
ad45db14 »
2011-03-29 Initial import of iPad Notes
383
b22fdbb4 »
2011-03-30 80 character line length
384 1. Copy your program to the server you want to run it on. If you're using git,
385 this probably just means to clone the repository from another server or service
fe54fa90 »
2011-03-30 More editing
386 like [GitHub][].
387
388 [GitHub]: http://github.com/
ad45db14 »
2011-03-29 Initial import of iPad Notes
389
b22fdbb4 »
2011-03-30 80 character line length
390 2. Assuming your project contains a 'server.js' file, navigate to the directory
391 containing it, then type:
ad45db14 »
2011-03-29 Initial import of iPad Notes
392
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
393 ~~~ {.shell}
394 $ screen
395 $ node server.js
396 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
397
b22fdbb4 »
2011-03-30 80 character line length
398 This invokes your 'server.js' program inside a so called screen session. Screen
09134a7d »
2011-03-30 More typos
399 is a tool that provides you with a shell that remains its state, even when you
400 close the terminal app you used to login to your server.
ad45db14 »
2011-03-29 Initial import of iPad Notes
401
e1ee180f »
2011-03-30 Typo
402 So you can now safely close your terminal app, and your 'server.js' will
b22fdbb4 »
2011-03-30 80 character line length
403 continue running. If you want to monitor it, you can log into your server again,
404 and type:
ad45db14 »
2011-03-29 Initial import of iPad Notes
405
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
406 ~~~ {.shell}
407 $ screen -r
408 ~~~
ad45db14 »
2011-03-29 Initial import of iPad Notes
409
410 This will reconnect you to the backgrounded shell running your program.
411
b22fdbb4 »
2011-03-30 80 character line length
412 However, this approach is only recommended for experimental deployments. If your
413 node applications crashes at some point, screen will not try to restart it, so
414 don't use this method for production applications.
ad45db14 »
2011-03-29 Initial import of iPad Notes
415
416 ### Joyent no.de
417
a8144cd3 »
2011-03-30 Initial editing for beginner guide.
418 *Work in progress, please come back later ...*
419
Something went wrong with that request. Please try again.