

A Quick Node.js Jumpstart - renaebair
http://intridea.com/2011/4/8/nodejs-jumpstart

======
joevandyk
You should be using coffeescript though, makes the javascript in the example
look like:

    
    
      fs = require('fs')
    
      for i in [1..3]
        fs.readFile 'test.coffee', (err, data) ->
          console.log("#{i}. Finished reading file")
        console.log("#{i}. doing something important...")
    
    

And if you want to get rid of that "gotcha" (which is a javascript one, not a
node.js one):

    
    
      fs = require('fs')
    
      for i in [1..3]
        do (i) ->
          fs.readFile 'test.coffee', (err, data) ->
            console.log("#{i}. Finished reading file")
        console.log("#{i}. doing something important...")
    

This will output the number you'd intuitively inside the file read callback
function.

~~~
nolite
do you need to preprocess the coffescript somehow before node runs it, or will
node run it directly?

~~~
marcomonteiro
You have to compile it.

<http://jashkenas.github.com/coffee-script/>

~~~
nonames
Not necessarily. The distributed `coffee` script will automatically compile
before running the script. Plus it shows you coffeescript-friendly tracebacks
on error.

~~~
marcomonteiro
Whether I manually compile it or it's automatically compiled it still has to
be compiled which was the question I was responding to. Which is to say
Node.js will not run CoffeeScript directly.

------
checker
As someone who has read about node but never actually looked at any demo code,
this was a pretty good example. Now I understand what all the fuss is about!

------
abtinf
If folks have node questions, we would be happy to help - wompt is built on
node.

<http://wompt.com/chat/nodejs>

~~~
ryanfitz
what is a good way to share something like a db connection between modules?

~~~
_pdeschen
I think using require would do the trick. Something along the line like:

/* foo.js _/

var date = new Date();

exports.Date = date;

/_ bar.js _/

var foo = require('./foo.js');

exports.Date = foo.Date;

/_ foobar.js */

var foo = require('./foo.js');

var bar = require('./bar.js');

console.log(foo.Date);

console.log(bar.Date)

> console.log(foo.Date);

Sat, 09 Apr 2011 01:05:12 GMT

> console.log(bar.Date)

Sat, 09 Apr 2011 01:05:12 GMT

We are dealing with the same date variable between modules.

[edited. not a good place to share code. gist over github:
<https://gist.github.com/910994>]

