everyware
v0.0.4
Published
a tiny middleware library for q promises - EXPERIMENTAL
Downloads
4
Readme
everyware is a tiny experimental Q promise middleware library. It is intended mainly for middleware library authors, not application developers — it could be used to implement a library like jaque, but does not replace such a library.
In everyware, just as in Q-JSGI, an application is a function of one
argument (the env
) which returns a response or a promise of a
response. A middleware is a function of one argument (the "next"
application) which returns an application.
A silly example:
function notFound(env) {
return {status: 404, body: "not found"};
}
function foo(env) {
return {status: 200, body: "foo"};
}
function route(url, app) {
return function(next) {
return function(env) {
if (env.url == url) {
return app(env);
} else {
return next(env);
}
};
};
}
// w takes an app and variadic middleware, and returns an app with the
// middleware applied
var w = require('everyware');
var app = w(notFound, route("/foo", foo));
everyware does not assume that the middleware stack is used for handling HTTP requests. It is meant to be reused in different contexts. The middleware pattern is useful for more than HTTP — everyware provides the basics so you can easily start using this pattern in your domain.
everyware is currently used in backbone-live to build Backbone.sync implementations using functional Q middleware.
everyware strives for simplicity over convenience. JSGI style for middleware may seem less convenient than connect style, but it is simpler.
everyware makes an additional simplifying assumption that middlewares
are functions of exactly one argument, requiring another closure to
pass other arguments to a middleware (url
and app
in the route
example above), to work around the lack of macros in JavaScript.