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

Node.Cincy Aug. 8th, 2012

Modulus
August 08, 2012

Node.Cincy Aug. 8th, 2012

Brandon Cannaday from Modulus provides a high level overview of Node.js and discusses a solution to callback management. Video of this presentation available at https://modulus.io/node-presentations

Modulus

August 08, 2012
Tweet

More Decks by Modulus

Other Decks in Programming

Transcript

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

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

    and PHP except for JavaScript • Google’s V8 Engine • Single threaded, non- blocking Thursday, August 9, 12
  3. USE CASES • Good at: • Data-driven APIs • Real-time

    apps • Websites • Scaling • Bad at: • Data crunching Thursday, August 9, 12
  4. 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, August 9, 12
  5. 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, August 9, 12
  6. 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, August 9, 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; 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, August 9, 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) { 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, August 9, 12
  9. 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, August 9, 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); 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, August 9, 12
  11. 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, August 9, 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(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, August 9, 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(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, August 9, 12
  14. • STEP 1: npm install express • STEP 2: npm

    install async SUMMARY Thursday, August 9, 12