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

Node.js for mobile developers

Node.js for mobile developers

Quick and dirty Node.js overview given to the mobile team at Tumblr on 4/30/2014. This doesn't have anything to do with mobile per se, but centers on why developers who rarely have time to do web development would be well suited to pick up Node.js for the times that they do.

These slides were creating using Deckset (http://www.decksetapp.com), and the source can be found here: https://gist.github.com/irace/ec0ce0faaadf00ccaddb

Bryan Irace

May 03, 2014
Tweet

More Decks by Bryan Irace

Other Decks in Programming

Transcript

  1. Node.js for mobile
    developers
    Bryan Irace

    View Slide

  2. What is Node.js?
    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.

    View Slide

  3. Not a web framework or
    language

    View Slide

  4. Components
    4 V8 (Google's JavaScript engine)
    4 libuv (platform Independent I/O bindings)
    4 Core libraries (networking, modules, REPL, streams,
    logging)
    4 OpenSSL, etc.

    View Slide

  5. What's it good at?
    4 I/O
    4 Command line tools
    4 Not CPU-bound work

    View Slide

  6. Everything is asynchronous

    View Slide

  7. JavaScript
    4 Easy to learn
    4 Powerful
    4 Runs everywhere (you need to know it anyway...)
    4 Always getting faster
    4 Improving pretty rapidly
    4 Lots of good libraries

    View Slide

  8. NPM
    ~70K modules

    View Slide

  9. package.json
    {
    "name": "lunchr",
    "version": "1.0.0",
    "dependencies": {
    "express": "3.4.3",
    "request": "2.27.0",
    "underscore": "1.6.0"
    }
    }

    View Slide

  10. Importing
    var request = require('request');
    var _ = require('underscore');
    var qs = require('querystring');
    var express = require('express');

    View Slide

  11. Modules
    4 jQuery
    4 Underscore
    4 Express
    4 Async
    4 Request

    View Slide

  12. Express
    var express = require('express');
    var app = express();
    app.get('/', function(req, res){
    res.send('Hello, world');
    });
    app.listen(3000);

    View Slide

  13. Async
    async.parallel([
    function (callback) {
    ywa('1519698779', 'BOOKMARK_10135', callback);
    },
    function (callback) {
    play('com.tumblr', callback);
    }
    ], function (err, results) {
    if (err) {
    callback(err);
    }
    else {
    var ywa_data = results[0]
    , play_rating = results[1]
    // TODO
    }
    }

    View Slide

  14. Request
    request({
    url: 'https://rink.hockeyapp.net/api/2/apps/' + app_id + '/statistics',
    headers: {
    'X-HockeyAppToken': '61c40d6b6fac43d880e97c0bd22b4903'
    }
    }, function (err, response, body) {
    if (err) {
    callback(err);
    }
    else {
    var versions = JSON.parse(body)['app_versions'];
    // TODO
    }
    }

    View Slide

  15. jQuery
    $('tr.stathead').remove();
    $('tr').removeAttr('align').removeAttr('class');
    $('tr:first').find('td').replaceWith(function () {
    return $('')
    .html($(this).contents())
    .attr('data-week', index_of($(this)));
    });

    View Slide

  16. Underscore
    _.chain([1, 2, 3, 200])
    .filter(function(num) {
    return num % 2 == 0;
    })
    .map(function(num) {
    return num * num
    })
    .value();

    View Slide

  17. shell.js
    require('shelljs/global');
    if (!which('git')) {
    echo('Sorry, this script requires git');
    exit(1);
    }
    // Copy files to release dir
    mkdir('-p', 'out/Release');
    cp('-R', 'stuff/*', 'out/Release');
    // Replace macros in each .js file
    cd('lib');
    ls('*.js').forEach(function(file) {
    sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
    sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
    sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
    });
    cd('..');
    // Run external tool synchronously
    if (exec('git commit -am "Auto-commit"').code !== 0) {
    echo('Error: Git commit failed');
    exit(1);
    }

    View Slide

  18. Hosting
    4 Easy to install
    4 Windows
    4 OS X (just a .dmg file)
    4 Easy to host
    4 Heroku
    4 Windows Azure

    View Slide

  19. Debugging
    4 node-inspector

    View Slide