What is node.js?

Andy Balaam
artificialworlds.net/blog

node.js is

A JavaScript engine

$ node
> console.log( "Hello, world" );
Hello, world
> 2+3
5
> 0 == ""
true

A JavaScript engine

A JavaScript engine

# namethatday.js

var weekday = require( "weekday" );

var day = weekday( process.argv[2] );

console.log( day.name );

A JavaScript engine

$ node namethatday.js 5
Friday

A set of libraries

A set of libraries

A set of libraries

concat-array

Takes a string and an array of strings and concatenates the first string to each string in the array.

A set of libraries

var ca = require('concat-array');

var arr = ['shrimp', 'tron'];
console.log(ca.concatArray('jumbo', arr));

# prints [ 'jumbo shrimp', 'jumbo tron' ]

A set of libraries

var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('Hello World');
});

app.listen(3000);

A set of libraries

A set of libraries

var express = require('express');
var app = express();

app.use(express.basicAuth("testUser", "testPass"));

app.get('/', function(req, res) {
  res.send('Hello World');
});

app.listen(3000);

Going to blow your mind

"Everything runs in parallel, except your code"

Going to blow your mind

"Everything runs in parallel, except your code"


Going to blow your mind

"Everything runs in parallel, except your code"

Pros:

Going to blow your mind

"Everything runs in parallel, except your code"

Cons:

Going to blow your mind

var fs = require('fs');

function doSomething(err, contents) {
    if (err) throw err;
    console.log(contents);
}

fs.readFile('myfile.txt', 'utf8', doSomething);

Going to blow your mind

var fs = require('fs');

fs.readFile('myfile.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});

Going to blow your mind

$ node cp.js myfile.txt myfile2.txt
Copied 4 bytes.

Going to blow your mind

copyFile( process.argv[2], process.argv[3],
    function(err, length) {
        if (err) console.log(err);
        else console.log( "Copied " + length + " bytes." );
    }
);

Going to blow your mind

function copyFile(infn, outfn, callback) {
    fs.readFile( infn, function(err, data) {
        if (err) callback(err);
        else fs.writeFile( outfn, data, function(err) {
              if (err) callback(err);
              else callback(null, data.length);
}); }); }

Going to blow your mind

                                                            });
                                                        });
                                                    });
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

Tips for writing node code

  1. Don't forget you are writing code

Tips for writing node code

  1. Don't forget you are writing code
    • Write short functions
    • Write tests
    • Give things names
  2. There is no 2

More info

Videos youtube.com/user/ajbalaam
Twitter @andybalaam
Blog artificialworlds.net/blog
Projects artificialworlds.net