15. ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>
16. ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>
jQuery.ajax({
success: function(data) {
// do something with data
}
})
17. ASYNC I/O
<?php
// I block!
$data = file_get_contents($file)
// do stuff with $data
?>
jQuery.ajax({
success: function(data) {
// do something with data
}
})
// I don’t block!
var fs = require('fs')
fs.readFile(file, function(err, data) {
// i can has data
})
18.
19. REQUIRE
• Node uses the commonJS module system
• returns the exported API of a module
// math.js
exports.add = function() {
var sum = arguments[0];
for (var i=1; i<arguments.length; i++) {
sum += arguments[i];
}
return sum;
};
// app.js
var add = require('math').add;
add(4, 8, 15, 16, 23, 42); //-> 108
26. BUILT ON NODE
// app.js
require.paths.unshift('path/to/express/lib')
require('express')
get('/', function(){
this.redirect('/hello/world')
})
get('/hello/world', function(){
return 'Hello World'
})
run()
$ node app.js
Express started at http://localhost:8000/ in development mode