Skip to content

Instantly share code, notes, and snippets.

@notmatt
Created May 27, 2012 19:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notmatt/2815639 to your computer and use it in GitHub Desktop.
Save notmatt/2815639 to your computer and use it in GitHub Desktop.
Polyglotconf ad-hoc "intro to node.js" talk

Polyglotconf intro to node.js

If you're interested in learning more in person, VanJS (@vanjs) often has node-related content.

And like Rob and Brock mentioned in the session, they're starting up the Vancouver Node Brigade (@NodeBrigade) for a more node-focused meetup.

Now, as promised, links from the session and some expanded notes. All questions, comments, and pull requests are most welcome.

Installation & Hello World

Simple

The official site has installers for Win/Mac and a source tarball.

Latest (or old versions)

git clone https://github.com/joyent/node.git
sudo make install

Previous versions are available by checking out the appropriate tag.

Both latest and stable?

I use nave, which is a version manager for node that works via subshells. It handles building and installing the versions, it's smart about npm versions (pre-0.6 it wasn't included with node), and doesn't interfere with a standard installation of node. (Unix-ish only, not sure of comparable Windows solutions, sorry)

Hello, world!

Node's 'hello world' is extremely short:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

This starts a server on port 1337, responding to http requests with a simple 'Hello World`. Interesting observations from this might include:

  • http is a first-class citizen
  • core node provides a fairly low-level interface to http

This is great for a lot of uses, but is definitely a lower level of abstraction than you might be used to coming from other stacks.

On the one hand, this is part of what makes node well-suited for systems-level programming (as we use it at Joyent). On the other hand, this gives library and framework authors a huge amount of latitude to implement abstractions on top of node.

Basically, it's win-win. Stay in the core to control everything. Use community code when you want to work at a higher level. Which brings us to...

npm and the ecosystem

npm is node's package manager, and is package management done very well indeed. Basic usage is simple: npm install FOO, and by default it functions locally to the project, which avoids conflicting dependencies between projects. npm's website is quite basic, but its built-in help is excellent: npm help install.

The same question comes up whenever you join a new open-source community: how do I tell a weekend hackathon project from production-tested code? What's "normal" and what's off the beaten track?

General tools

json - Unix-style command-line manipulation of JSON data. If you deal with JSON, especially via an API, then you should install this right now, even if you never ever touch node again: npm install -g jsontool.

bunyan - machine-readable logging in JSON. Obviously works extremely well with the abovementioned json. Need convincing that machine-readable logs make sense? See Paul Querna's thoughts on logging, or the node.js blog on Bunyan.

browserify - found a node module you wish could work client-side? This is your solution.

Flow control

Managing callbacks, nested callbacks, and asynchronous flow control is a common bottleneck when writing node applications. Some of the best advice is to write your own flow control library; even a small, partial implementation will give you a lot of insight, and handling callbacks will be second nature. That said, there are libraries to help you:

async - commonly recommended, almost a de facto standard. Provides an API of async versions of common array operations, very composable. Support various plugins, helpers.

queue - a minimal approach to the problem (554 bytes!); provides simple async queue, with tunable parallelism. Bonus: also works client-side.

Web apps, APIs

Express - I suspect (but have no data) that it's the most popular framework in the node.js world. Provides routing, broad templating support, concise request and response handling, is easily extensible. For rubyists, this is similar to sinatra.

Flatiron - from nodejitsu, a framework composed of several independent modules, each solving one particular problem in the stack.

Restify - REST-specific API framework, with an emphasis on the features you want to build that kind of service: versioning, observability, explicit control of http interactions.

tako - a functional web framework. Should probably be considered experimental, but is too interesting not to mention.

Auth

everyauth - this was mentioned in the session; adds third-party authorization to Express and Connect apps, for just about every value of '3rd-party' you'd ever want.

request - is a simplified wrapper of node's core http request, and provides OAuth support as well.

Data layer

Bindings for anything you can think of. Mentioned in the session:

MVC?

I don't have direct experience in this area, but there is some data modelling provided by Resourceful (part of flatiron, mentioned above). Mongoose provides an ORM on top of MongoDB.

There was an 'MVC shootout' session earlier in the day (though the session didn't produce any favorites, just a consensus that what works for your is the best choice). Two mentioned there that are compatible with node are: Derby provides a combined server/client MVC. Knockout provides MVVM.

There are a number of rails-inspired frameworks for node, but as far as I know none have any particular traction (I'd welcome anyone else's experience in this area).

Testing

Testing frameworks proliferate; they're relatively easy to write, and no one testing framework will solve everyone's problem or provide the level of testing they want. One good solution for testing is to see what your other modules are using, and go with what you like. Some of the possibilities here are:

I'd strongly recommend looking at node-tap - test anything protocol for node, which can be used on its own, or to produce TAP-compliant output from other test setups.

Communication/sockets

One of the earliest hits around node.js was socket.io - providing a high-level abstraction over WebSockets. It's worth trying out.

A couple of other tools in this space were mentioned in the session: now.js uses a similar method for data binding and RPC, and Faye exposes a pub/sub model.

Deployment - get it out there.

Simple, node-focused services are the ideal choice to get started. These are ones I know have free options (and you may already be using for other platforms):

Daniel Shaw (@dshaw) has an excellent set of slides focused around deploying node in production, from handling uncaught exceptions, to logging, multiple processes, and adding a repl to a running app: http://dshaw.github.com/2012-05-jsday/#/

Elsewhere

Best stuff cribbed from the official site:

http://nodeup.com/ - node-focused podcast, with annotations on what's discussed, lots of links and solid opinions. If anything above contradicts what they say, you should probably go with them.

https://groups.google.com/group/nodejs - Google Group/mailing list.

#node.js on irc.freenode.net

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