#Node Modules are AWESOME Andrew Kim Consumer Web Developer
#Big Apps require big code
- Spaghetti Code *http://cdn-ecomm.dreamingcode.com/public/149/images/spah1-149-19131-1.jpg
- Bloated Codebase
- HUGE UNREADABLE FILES
- Complex and confusing logic
- Looking for a bug in a haystack.
- I wish I had a libary that did just this.
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
- 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.
- keep modules small
- but...
Question: What are the benefits to small modules?
3 Files
- package.json
- Readme.md (shows up on npm)
- index.js
- 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
- http://justbuildsomething.com/node-js-best-practices/#1
- Be a good citizen. Other people will appreciate you
- database modules are a good example of asynchronous-ness(ity)
// 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?
- referential transparency (fancy programming talk)
- No global state in modules (pass configs instead) ** example: api keys
- Don't expose global state
- 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?