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

Node.js Introduction for Makers Academy

Node.js Introduction for Makers Academy

Presentation show to the guys at Makers Academy about Node.js

Simon Wood

May 16, 2013
Tweet

More Decks by Simon Wood

Other Decks in Programming

Transcript

  1. What is Node.js? Node.js is a platform built on Chrome's

    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. nodejs.org
  2. History & Details Node.js was created by Ryan Dahl starting

    in 2009 Node.js is a packaged compilation of Google's V8 JavaScript engine and a core library, which is itself primarily written in JavaScript Current stable version is 0.10.6
  3. Why Javascript server side? Can use the same language you're

    using for client side app. One language deployed everywhere Lets us share code between the browser and the backend It is event-based so it's like Ajax only server side It has low resource overheads It's fast. Really fast!
  4. How to install it Using homebrew: brew install node Using

    macports: port install nodejs or install it from a compiled package
  5. HTTP Hello World hello.js var http = require( 'http' );

    var server = http.createServer(); server.on( 'request', function( req, res ) { res.end( 'Hello World!' ); } ); server.listen( 1337, '127.0.0.1' ); console.log( 'Server running at http://127.0.0.1:1337' ); terminal /node-demos$ node hello.js Server running at http://127.0.0.1:1337
  6. Event driven jQuery - Client Side $( 'button' ).on( 'click',

    function( event ) { document.write( 'Event driven' ); } ); Node.js - Server Side server.on( 'request', function( req, res ) { res.write( 'Event driven' ); } );
  7. Non-blocking I/O Blocking I/O var post = db.query( 'select *

    from posts where id = 1' ); doSomethingWithPost( post ); doSomethingElse(); Non-blocking I/O callback = function( post ) { doSomethingWithPost( post ) } db.query(' select * from posts where id = 1', callback ); doSomethingElse();
  8. Packages & Extensions Packages written in Javascript Either small packages

    of tools code, frameworks, or libraries for talking to DB's etc Packages best managed with NPM Extensions written in C++ Used to extend the core functions available to Node.js Commonly used to write libraries for DB's etc
  9. What Node.js is good for? Long polling Keeping lots of

    concurrent connections open Having same code server and client side Networking operations Real time applications Great for chat applications, polling applications like gmail, games servers, API's, API Gateways, proxy servers
  10. When not to use Node.js Still use traditional languages like

    PHP, Python, Ruby for tradition web solutions You would not rewrite wordpress in Node.js It could do it, but it is not the best choice