Tim Caswell changelog.com/posts

Connect - Middleware for NodeJS

For the past few weeks Tim Caswell and TJ Holowaychuk have been busy developing a new middleware engine for Node.JS. This was unveiled recently at SWDC in Stockholm, Sweden and TXJS in Austin, Texas, and released by their employer, Ext JS, as a gift to the open source community.

Connect is a middleware system for node that makes it easy to mix and match utility modules into powerful production level applications. Have you ever wanted to add static file serving to your node app? No problem, it’s a one liner in Connect! Do you want gzip encoding, ram-caching, and 304 responses for super fast servers? Again, it’s just a line each to add these to Connect’s ruby-rack-like configuration call.

A stackup config for a Connect based blog might look something like this:

var controlled = ["/console/", "/files/", "/messages/"];

module.exports = require('./lib/connect').createServer([
    // We want to log all http traffic
    {filter: "log"},
    // Add cookie based sessions to the controlled routes
    {filter: "session", routes: controlled},
    // Make sure the user is authenticated
    {filter: "authentication", routes: controlled, param: {}},
    // Restrict access to controlled pages by user rules
    {filter: "authorization", routes: controlled, param: {}},
    // Listen for publish subscribe messages in real-time
    {provider: "pubsub", route: "/messages/", param: {}},
    // This is a logic endpoint, it's ext-direct rpc protocol
    {provider: "direct", route: "/console/", param: {}},
    // Use conditional GET to save on bandwidth
    {filter: "conditional-get"},
    // Cache all rest and static responses
    {filter: "cache"},
    // Gzip all resources when it makes sense
    {filter: "gzip"},
    // This is another logic endpoint, it's a rest-style interface to files
    {provider: "rest", route: "/files/", param: {}},
    // Finally serve everything else as static files
    {provider: "static", root: __dirname + "/public"},
    // Show pretty pages for exceptions
    {filter: "error-handler"}
]);

For more information see the recent article on howtonode.org about Connect.

[Source on GitHub] [Blog post]


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00