My Intro to Node talk at CodeShow.1 in Charleston, SC. I talk about what node is, how non-blocking I/O works, give a solid review of JavaScript semantics, promote small modules, etc.
over a dozen JavaScript and node.js conferences around the world. Started over a hundred open source projects on github. I love teaching programming and new tech! @creationix
JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non- blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
operating system. It’s especially useful for anything that involves heavy I/O It’s built using a fast JavaScript engine bound to an efficient event-loop library.
your desk. (3 seconds) L2: Getting a book from the shelf across the room. (14 seconds) RAM: Walking across the office. (4 minutes) DISK: Roaming the Earth for 16 months! NETWORK: 7 year journey.
var net = require('net'); // Create a plain socket server that echoes input. var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket); }); // Bind and listen on a TCP port. server.listen(1337, '127.0.0.1');
a server instance that accepts http connections var server = http.createServer(function (req, res) { // Respond to every request with the same message. res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': 12 }); res.end('Hello World\n'); }); // Start the server listening on a TCP port. server.listen(1337, '127.0.0.1');
more times. A single emitter may emit different types of events. The “error” event is special and must be handled or it will crash. Usually represent human interaction or stream events.
easy var EventEmitter = require('events').EventEmitter; var bob = new EventEmitter(); bob.on("speak", function (message) { console.log("Bob says " + message); }); bob.emit("speak", "Hello World");
from EventEmitter function JoyStick(id) { // Call the super constructor EventEmitter.call(this); // Do your own custom logic too this.openDevice(id); // ... } Joystick.prototype.__proto__ = EventEmitter.prototype;
JavaScript is essential to understanding Streams represent a stream of data, usually through a network socket or file I/O. Streams can be piped together like Unix commands.
top scope var name = "Tim Caswell"; var age = 30; var isProgrammer = true; var likesJavaScript = true; // Test to see if the two variables are the same. isProgrammer === likesJavaScript; //-> true
name; }; } var description1 = makeClosure("Cloe the Closure"); var description2 = makeClosure("Albert the Awesome"); console.log(description1()); console.log(description2());
description: function () { return this.name; } }; var description = Lane.description; var Fred = { description: Lane.description, name: "Fred the Functor" }; // Call the function from four different scopes console.log(Lane.description()); console.log(Fred.description()); console.log(description()); console.log(description.call({ name: "Zed the Zetabyte" }));
parent class Rectangle.prototype.toString = function toString() { return this.constructor.name + " a=" + this.getArea() + " p=" + this.getPerimeter(); }; var rect = new Rectangle(6, 4); var sqr = new Square(5); console.log(rect.toString()) console.log(sqr.toString())