• Node.js and High-performance web site consulting and training • Walmart, MySpace, Joyent, Yahoo!, NASA, Tesco, etc • Node.js contributor • Lead author of "Node: Up and Running" Wednesday, May 30, 12
text "I'm learning Node" • Change the HTTP response to HTML and return your text in an HTML page • Return the User-Agent string from the browser as part of your HTML page Wednesday, May 30, 12
items with a form // (use the bodyParser() middleware for this) var items = req.body.items; console.log(items); res.send('logging'); }); Wednesday, May 30, 12
•Serve two different pages based on value of the query string parameter "page" •Create a redirect from /old to /new •Set a cookie on the client Exercise Wednesday, May 30, 12
var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { io.sockets.emit('this', { will: 'be received by everyone'}); socket.on('private message', function (from, msg) { console.log('I received a private message by ', from, ' saying ', msg); }); socket.on('disconnect', function () { sockets.emit('user disconnected'); }); }); Wednesday, May 30, 12
route which loads a page that includes socket.io •Send "hello world" to the client sockets •Make the client respond with "thanks" and disconnect from the server Exercises Wednesday, May 30, 12
= require('os').cpus().length; if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); } } else { var express = require('express') , app = express.createServer(); var sio = require('socket.io') , RedisStore = sio.RedisStore , io = sio.listen(app); app.listen(8080); app.get('/', function(req,res) { res.sendfile(__dirname + '/index.html'); }); // Somehow pass this information to the workers io.set('store', new RedisStore); // Do the work here io.sockets.on('connection', function (socket) { socket.on('chat', function (data) { console.log(data); socket.broadcast.emit('chat', data); }) }); } Wednesday, May 30, 12
first tick ^ Error: Uncaught, unspecified 'error' event. at EventEmitter.emit (events.js:47:15) at Object.<anonymous> (/ Users/you/y-u-no-listen-for-errors.js:5:9) at Module._compile (module.js:404:26) at Object..js (module.js:410:10) at Module.load (module.js:336:31) at Function._load (module.js:297:12) at Array.<anonymous> (module.js:423:10) at EventEmitter._tickCallback (node.js:126:26) Wednesday, May 30, 12
host: 'www.google.com', path: '/', port: 80, method: 'POST' }, function (response) { // Do stuff with the response here }); } catch(e) { //exception handling } Wednesday, May 30, 12
path: '/', port: 80, method: 'POST' }, function (response) { // Do stuff with the response here }); req.on('error', function (err) { //safely handle this if possible }); Wednesday, May 30, 12
Use emitter.setMaxListeners() to increase limit. Trace: at Pool.<anonymous> (events.js:101:17) at Object.proxyRequest (~/http-proxy/lib/node-http-proxy.js:185:7) at Server.<anonymous> (/Users/some-user/myapp.js:14:9) at Server.emit (events.js:45:17) at HTTPParser.onIncoming (http.js:1078:12) at HTTPParser.onHeadersComplete (http.js:87:31) at Socket.ondata (http.js:977:22) at Socket._onReadable (net.js:654:27) at IOWatcher.onReadable [as callback] (net.js:156:10) Wednesday, May 30, 12
= new events.EventEmitter(); setTimeout(function () { emitter.emit('done'); }, 2000); return emitter; } var doingIt = doSomethingThenTellMe(); doingIt.on('done', function () { console.log("Ok, it's done"); }); // Why are you using `.on()`? // You only expect this event to fire once. Wednesday, May 30, 12
shutdown script # We found $HOME is needed. Without it, we ran into problems export HOME="/root" exec /usr/local/bin/node /var/noderoot/index.js 2>&1 >> /var/log/node.log end script Wednesday, May 30, 12