The middleware architechture is strange beast. On one hand everybody knows about it and knows how to use it. On the other hand, it seems there are only a handful of modules that take advantage of it.

ware (GitHub: segmentio/ware, License: MIT) is a poster child for a Node.js module. It’s just about 80 lines large, maintained by abunch of folks at Segment, has 10k daily downloads and just under 100 stars on GitHub. It’s sole purpose is to help you easily create your own middleware layer.


npm install ware


The API is very straight forward, in fact lets make something that looks like Express.js

var ware = require('ware');
var app = ware();

app.use(function(req, res, next) {
  if (!req.name) {
    return next(new Error('Missing `name`.'));

  res.x = 'Hello ' + req.name;
  setTimeout(next, 50);

app.use(function(req, res, next) {
  res.y = '[' + res.x + ']';

app.run({name: 'Alex'}, {}, function(err, req, res) {

app.run({}, {}, function(err, req, res) {

There’s also a built in support for generators (on the server for now).

var ware = require('ware');

var middleware = ware()
  .use(function(obj) {
    obj.url = 'http://google.com';
  .use(function *(obj) {
    obj.src = yield http.get(obj.url);

middleware.run({ url: 'http://facebook.com' }, function(err, obj) {
  if (err) throw err;
  obj.src // "obj.url" source

What Else?

Checkout the runnable example and github example repository.

Post navigation

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *