Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.Indy Sep. 26th, 2012

Modulus
September 26, 2012

Node.Indy Sep. 26th, 2012

Brandon Cannaday from Modulus provides a high level overview of Node.js and discusses a solution to callback management.

Modulus

September 26, 2012
Tweet

More Decks by Modulus

Other Decks in Programming

Transcript

  1. HISTORY • Created by Ryan Dahl in 2009 • Created

    to address non- currency issues in existing frameworks • Currently owned and maintained by Joyent and Isaac Schlueter http://www.youtube.com/watch?v=SAc0vQCC6UQ Thursday, September 27, 12
  2. WHAT IS IT? • Server-side scripting framework • Like Rails

    and PHP except for JavaScript • Google’s V8 Engine • Single threaded, event- driven, non-blocking Thursday, September 27, 12
  3. MOMENTUM • New and shiny • JavaScript end-to-end • Community

    • Performance Thursday, September 27, 12
  4. THE THREADPOOL requests Traditional requests Node.js Thread Application Filesystem /

    OS Threadpool Thread Thread Threadpool Thread Thread Thread Application Filesystem / OS Thursday, September 27, 12
  5. fs.readFile(‘myFile.txt’, readComplete) The ‘real’ work Function to call when it’s

    done (the callback) NON-BLOCKING All ‘real’ work is done async Thursday, September 27, 12
  6. USE CASES • Good at: • Real-time • Scaling •

    Data-driven APIs • Websites • Bad at: • Data crunching Thursday, September 27, 12
  7. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; }); app.listen(80); Get posted user CALLBACK HELL User Registration Example Thursday, September 27, 12
  8. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); Generate hash salt Get posted user CALLBACK HELL User Registration Example Thursday, September 27, 12
  9. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); Generate hash salt Get posted user Generate hash CALLBACK HELL User Registration Example Thursday, September 27, 12
  10. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); Generate hash salt Get posted user Save user to the DB Generate hash CALLBACK HELL User Registration Example Thursday, September 27, 12
  11. Async.js var express = require('express'), bcrypt = require('bcrypt'), db =

    require('database'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); var express = require('express'), bcrypt = require('bcrypt'), db = require('database'), async = require('async'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; async.waterfall([ // Functions to execute ], function(err, result) { // Called on any error or at end }); app.listen(80); Thursday, September 27, 12
  12. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); var express = require('express'), bcrypt = require('bcrypt'), db = require('database'), async = require('async'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; async.waterfall([ function(callback) { bcrypt.genSalt(callback); } ], function(err, result) { // Called on any error or at end }); app.listen(80); Async.js Thursday, September 27, 12
  13. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); var express = require('express'), bcrypt = require('bcrypt'), db = require('database'), async = require('async'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; async.waterfall([ function(callback) { bcrypt.genSalt(callback); }, function(salt, callback) { bcrypt.hash(user.password, salt, callback); } ], function(err, result) { // Called on any error or at end }); app.listen(80); Async.js Thursday, September 27, 12
  14. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); var express = require('express'), bcrypt = require('bcrypt'), db = require('database'), async = require('async'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; async.waterfall([ function(callback) { bcrypt.genSalt(callback); }, function(salt, callback) { bcrypt.hash(user.password, salt, callback); }, function(hash, callback) { user.passHash = hash; db.saveUser(user, callback); } ], function(err, result) { // Called on any error or at end }); app.listen(80); Async.js Thursday, September 27, 12
  15. var express = require('express'), bcrypt = require('bcrypt'), db = require('database'),

    app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; bcrypt.genSalt(function(err, salt) { if(err === null) { bcrypt.hash(user.pwd, salt, function(err, hash) { if(err === null) { user.passHash = hash; db.saveUser(user, function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { // Failed to save user res.send(err); } }); } else { // Failed to generate hash res.send(err); } }); } else { // Failed to generate salt res.send(err); } }); }); app.listen(80); var express = require('express'), bcrypt = require('bcrypt'), db = require('database'), async = require('async'), app = express(); app.post('/user/register', function(req, res) { var user = req.body.user; async.waterfall([ function(callback) { bcrypt.genSalt(callback); }, function(salt, callback) { bcrypt.hash(user.password, salt, callback); }, function(hash, callback) { user.passHash = hash; db.saveUser(user, callback); } ], function(err, result) { if(err === null) { res.send('SUCCESS!'); } else { res.send(err); } }); app.listen(80); Async.js Thursday, September 27, 12
  16. • STEP 1: npm install express • STEP 2: npm

    install async REMEMBER THIS Thursday, September 27, 12