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

Domains in node 0.8

Domains in node 0.8

A quick presentation I gave at HolidayExtras on using the new domain feature in node.js 0.8.

23968f0ca75b13463d2db5343e5c2096?s=128

Felix Geisendörfer

June 29, 2012
Tweet

Transcript

  1. Domains in node 0.8 A quick introduction Freitag, 29. Juni

    12
  2. 1 var app = require('express')(); 2 3 app.get('/search', function(req, res,

    next) { 4 doSearch(req.params, function(err, result) { 5 if (err) return next(err); 6 7 // trying to access non-existing property 8 if (result.foo.bar) { 9 console.log('does not work'); 10 } 11 }); 12 }); 13 14 app.listen(8080); Freitag, 29. Juni 12
  3. /Users/Felix/Desktop/domains/example.js:14 if (result.foo.bar) { ^ TypeError: Cannot read property 'bar'

    of undefined at /Users/Felix/Desktop/domains/example.js:14:19 at doSearch (/Users/Felix/Desktop/domains/example.js:3:5) at process.startup.processNextTick.process._tickCallback (node.js:244:9) Freitag, 29. Juni 12
  4. 1 process.on('uncaughtException', function(err) { 2 // Keeps the process from

    crashing 3 // But does not let you find out which `req` 4 // was causing the `err` 5 }); Freitag, 29. Juni 12
  5. Domains to the rescue! Freitag, 29. Juni 12

  6. 1 var createDomain = require('domain').create; 2 3 app.use(function(req, res, next)

    { 4 var domain = createDomain(); 5 6 domain.on('error', function(err) { 7 // alternative: next(err) 8 res.statusCode = 500; 9 res.end(err.message + '\n'); 10 11 domain.dispose(); 12 }); 13 14 domain.enter(); 15 next(); 16 }); Freitag, 29. Juni 12
  7. $ curl -i localhost:8080/search HTTP/1.1 500 Internal Server Error X-Powered-By:

    Express Date: Fri, 29 Jun 2012 10:50:18 GMT Connection: keep-alive Transfer-Encoding: chunked Cannot read property 'bar' of undefined Freitag, 29. Juni 12
  8. Questions? Freitag, 29. Juni 12

  9. Hiring node.js developers @ marcgreenstock @dan_jenkins Freitag, 29. Juni 12