Skip to content

Instantly share code, notes, and snippets.

@wookimiii
Created October 29, 2014 17:35
Show Gist options
  • Save wookimiii/ba81da219b4599e59562 to your computer and use it in GitHub Desktop.
Save wookimiii/ba81da219b4599e59562 to your computer and use it in GitHub Desktop.

#Node Modules are AWESOME Andrew Kim Consumer Web Developer


#Big Apps require big code


Objectives: Convince you to write node modules and become awesome

  • Embrace Node
  • Modules are easy to use
  • Modules are small

Time: 30 minutes


#Node is awesome! Did you say javascript on the server side?!


#What is node?

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.


#What is node good at? Networking stuff. Why? Because it's non-blocking. Asynchronous pattern, using callbacks and events.


#What is node not good at? Computationally heavy things. Could be changing... JS is fast becoming used for more than single page web apps. Lots of game engines being built in JS for the client side. (asm.js)


#Modules are awesome!


// Awesome module
// awesome.js
module.exports = function awesome() {
    return "awesome";
};


var awesome = require('./awesome.js')
awesome();


#NPM

Makes modules 2xAWESOME


#Modules are easy to install


npm install underscore

var map = require('underscore').map;
map([1,2,3], function square(i) {
    return i*i;
});

#NPM modules are easy to share! NPM is the twitter of code.


npm init
npm publish
// bask in the glory of your awesomeness

Modules Rule #1

  • keep modules small (UNIX Philosophy)
  • http://substack.net/how_I_write_modules When I isolate the problem down to the scope of a small module, it's much easier to see how that small piece fits in with the greater application objective.

Modules Rule #2

  • keep modules small
  • but...

Question: What are the benefits to small modules?


What makes a module

3 Files

  • package.json
  • Readme.md (shows up on npm)
  • index.js

Some Good Tips for Readability

  • declare dependecies (requires) at the top;
    • node caches requires
    • easy to debug
  • declare constants and module variables at the top
  • exports at the bottom
  • Comment what's private and public
  • Explanation at the top

Question: What are some of your practices that help you read/understand/maintain code?


#Modules should be asynchronous


    // Bad way to read a file synchronously
    var data = fs.readFileSync('/path/to/file');
    console.log(data);

    //Good way to read file asynchronously
    fs.readFile('/path/to/file', function (err,data){
        console.log(data);
    });

Question: What's other areas would asynchronous be useful? When would asynchronous not be a good idea?


Functional Modules

  • referential transparency (fancy programming talk)
  • No global state in modules (pass configs instead) ** example: api keys
  • Don't expose global state

Avoid 'this'

  • It causes confusion?
  • What is this?

function Twitter(apiKey) {
    this.apiKey = apiKey;
}

Twitter.prototype = {
    get: function (user, callback) {
        http.get("http://twitter.com", {
            auth: this.apiKey,
            user: user,
            success: callback
        }
    },
    retweet: function (user) {
        this.get(user, function (err, data) {
            http.post(twitterAPI, {
                message: "RT" + data.message,
                auth: this.apiKey
            });
            // ERROR - undefined apiKey
        });
    }
}

function Twitter(apiKey) {
    var apiKey = apiKey;
    function get(user, callback) {
        // ... get the user's tweet and callback
    }

    function retweet(user) {
        get(user, function(err, data) {
            http.post(twitterAPI, {
                message: "RT" + data.message,
                auth: apiKey
            });
        }
    }
}

#Testing is easy!


var test = require('tape');
var awesome = require('./awesome');

test('awesome test', function (t) {
    t.plan(1); // helps with asynchronous tests
    t.equal(awesome(), 'awesome');
})

#Testing (continued)

  • Modules means tests can be focused and concise
  • Testing is reliable

For example: if you're looking for a bug, and most of your code is in modules have that solid testing then you can focus on your app logic


#Refactorability

  • Easier to refactor modules themselves
  • Easier to refactor apps that use modules

var map = require('underscore').map;
var a = map([1,2,3], function (i) { return i*i*});

// now with an optimized module
var _ = require('lodash').map;
var b = map([1,2,3], function (i) { return i*i*});

// a and b are the same!

#Node modules are awesome!


Seriously

  • So easy
  • Maintanable
  • Readable
  • Reusable
  • Refactorable
  • Shareable (sharing is caring)
  • Awesome

Questions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment