{
  "name": "middler",
  "version": "0.7.1",
  "dss": "modified for case insensitive match",
  "description": "An embeddable middleware runner",
  "scripts": {
    "test": "make test"
  },
  "repository": {
    "type": "git",
    "url": "git://github.com/carlos8f/node-middler.git"
  },
  "keywords": [
    "middleware",
    "http",
    "server"
  ],
  "author": {
    "name": "Carlos Rodriguez"
  },
  "license": "MIT",
  "engines": {
    "node": ">= 0.6.0"
  },
  "devDependencies": {
    "mocha": "*",
    "superagent": "*",
    "benchmarx": "~0.2.2"
  },
  "dependencies": {
    "minimatch": "~0.2.12",
    "methods": "0.0.1",
    "path-to-regexp": "0.0.2"
  },
  "readme": "middler\n=======\n\nAn embeddable middleware runner\n\n[![Build Status](https://secure.travis-ci.org/carlos8f/node-middler.png?branch=master)](http://travis-ci.org/carlos8f/node-middler)\n\nIdea\n----\n\nmiddler is a flexible, tiny middleware runner for Node.js which can easily be\nembedded in an existing http server or even an existing middleware chain.\nAlso provided is [express](https://github.com/visionmedia/express)-like routing\nand [union](https://github.com/flatiron/union) compatibility. Best of all, the\ncode is readable, compact, tested, benchmarked, and MIT-licensed.\n\nEnjoy!\n\nInstall\n-------\n\n```\n$ npm install --save middler\n```\n\nBasic usage\n-----------\n\n```javascript\nvar middler = require('middler')\n  , server = require('http').createServer()\n\n// to attach a single handler to the server:\nmiddler(server, function (req, res, next) {\n  console.log(req.method, req.url);\n  next();\n});\n\n// calling middler(server) again will access the same middleware chain:\nmiddler(server)\n  // note: all methods are chainable!\n  .get('/', function(req, res, next) {\n    res.writeHead(200, {'Content-Type': 'text/plain'});\n    res.end('hello world!');\n  })\n  // since this handler is added last, it will run last:\n  .add(function (req, res, next) {\n    res.writeHead(404, {'Content-Type': 'text/plain'});\n    res.end('page not found...');\n  })\n\nserver.listen(3000);\n```\n\nHTTP Routing\n------------\n\nIt's easy to set up routes which will respond to certain methods and paths:\n\n```javascript\nmiddler(server)\n  .add('/', function (req, res, next) {\n    // handle any request to \"/\" path\n  })\n  .get('/robots.txt', function (req, res, next) {\n    // handle GET requests only\n    res.end('humans only!');\n  })\n  .first('/posts/*', function (req, res, next) {\n    // do some setup for any path starting with '/posts/'\n    next();\n  })\n  .post('/posts/:id', function (req, res, next) {\n    // req.params.id available\n  })\n  .put('/articles/*/*', function (req, res, next) {\n    // req.params is an array with 2 elements\n  })\n  .get('*.mydomain.com', '/', function (req, res, next) {\n    // vhost matcher: handle the root path on *.mydomain.com\n  })\n  .add('localhost', function (req, res, next) {\n    // vhost matcher: only handle requests to localhost\n  })\n```\n\nTips:\n\n- Paths must be either strings starting with `/`, or RexExp objects.\n- Other methods available: `delete`, `head`, `patch`\n\nmiddler is a middleware, too\n----------------------------\n\nNow, the coolest feature of middler which sets it apart from the rest:\nembeddability.\n\nEach middler instance has a `handler` property which allows you to use the entire\nchain as a single middleware handler!\n\nExample with [connect](https://github.com/senchalabs/connect):\n\n```javascript\nvar connect = require('connect')\n  , http = require('http')\n  , middler = require('middler')\n\nvar app = connect()\n  .use(connect.favicon())\n  .use(connect.logger('dev'))\n  .use(connect.static('public'))\n  .use(connect.directory('public'))\n  .use(connect.cookieParser('my secret here'))\n  .use(connect.session())\n  .use(middler()\n    .get('/', function (req, res, next) {\n      res.end('hello world!');\n    })\n    .add(function (req, res) {\n      res.writeHead(404);\n      res.end('page not found');\n    })\n    .handler\n  );\n\nhttp.createServer(app).listen(3000);\n```\n\nThis can be extremely useful if you want to:\n\n- Bundle your app's features as middler chains which can be optionally attached\n  to the main chain, similar to \"controllers\" in MVC language.\n- Create a vhost architecture which hands off requests to a sub-chain.\n- Write node modules which provide advanced middleware (respond to a variety\n  of methods/paths/etc), and can be attached directly to a server instance or\n  used with connect/express/flatiron.\n\nStack control\n-------------\n\nTo add handlers which should run first or last in the stack:\n\n```js\nmiddler(server)\n  .last(function (req, res, next) {\n    // this should run last (weight = 1000)\n  })\n  .first(function (req, res, next) {\n    // this should run first (weight = -1000)\n  })\n  .add(500, function (req, res, next) {\n    // numbers become weights -- give an arbitrary weight of 500, will run in-\n    // between the above handlers\n  })\n```\n\nTo remove handler(s) from the stack:\n\n```js\nfunction myHandler (req, res, next) {}\n\nmiddler(server)\n  .add(myHandler)\n  .add('/about', function (req, res, next) {\n    res.end('about us');\n  })\n  .add(function (req, res, next) {\n    res.end('page not found');\n  })\n\n// let's remove myHandler\nmiddler(server).remove(myHandler);\n\n// let's remove /about\nmiddler(server).remove('/about');\n\n// actually let's clear the whole thing\nmiddler(server).removeAll();\n```\n\nMultiple paths/methods/handlers\n-------------------------------\n\n```javascript\nmiddler(server)\n  .add(['get', 'post'], '/', function (req, res, next) {\n    // handle both GET and POST requests to \"/\"\n  })\n\n// Or add multiple handlers\nfunction bodyParser (req, res, next) { req.body = ... }\nfunction formHandler (req, res, next) { // do something with req.body ... }\n\nmiddler(server)\n  .post('/posts', [bodyParser, formHandler])\n  // or even the \"flat style\"\n  .post('/posts', '/comments', bodyParser, formHandler)\n```\n\nWhen multiple handlers are added, they execute in series when the other\nconditions match.\n\nHandling errors\n---------------\n\nIf your application encounters an error, pass an Error object to the `next`\ncallback. The rest of the middleware chain will not run, and what middler does\nwith the error is dependent on the following conditions:\n\n- If there are `error` event listener(s) on the middler instance, they will be\n  invoked with `err, req, res` and the error will not propagate further.\n- If there is no `error` listener:\n    - In the case of an embedded middler, the error will propagate to the parent\n      chain, i.e. `next(err)`.\n    - Otherwise, the default error handler will run which terminates the response\n      with `500 Internal Server Error` status and no body. The error and stack\n      trace will be printed to `process.stderr`.\n\nExample custom error handler:\n\n```js\nmiddler(server)\n  .on('error', function (err, req, res) {\n    res.writeHead(500, {'Content-Type': 'text/plain'});\n    console.error(err.stack || err);\n    res.end('sorry, blame it on Rackspace!');\n  })\n  .add(function (req, res, next) {\n    // next() can accept an error\n    next(new Error('whoops!'));\n  })\n```\n\nAlternate attach syntax\n-----------------------\n\n```js\nvar server = require('http').createServer();\nvar router = middler()\n  .add(function (req, res, next) {\n    // handle request...\n  })\n  .attach(server)\n\n// you can also detach!\nrouter.detach();\n\n// router can be attached do a different server now\n```\n\n[union](https://github.com/flatiron/union) compatibility\n--------------------------------------------------------\n\n```javascript\nmiddler(server)\n  .add(function () {\n    // this.req\n    // this.res\n    this.res.emit('next');\n  })\n```\n\nBenchmarks\n----------\n\nMy results: https://gist.github.com/3473117\n\n### App with single middleware, \"hello world\"\n\n```\n****************  middler (7646.63 rps)\n****************  connect (7198.48 rps)\n*******           union (3057.69 rps)\n```\n\n### App with 100 routes\n\n```\n****************  middler-routes (6870.59 rps)\n***************   express-routes (6335.41 rps)\n******            director-routes (2414.89 rps)\n```\n\n### Running your own benchmark\n\nIn the middler root, run:\n\n```bash\n$ make bench\n```\n\nBrought to you by [benchmarx](https://github.com/carlos8f/node-benchmarx).\n\n---\n\nDeveloped by [Terra Eclipse](http://www.terraeclipse.com)\n--------------------------------------------------------\nTerra Eclipse, Inc. is a nationally recognized political technology and\nstrategy firm located in Aptos, CA and Washington, D.C.\n\n[http://www.terraeclipse.com](http://www.terraeclipse.com)\n\nLicense: MIT\n------------\nCopyright (C) 2012 Terra Eclipse, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is furnished\nto do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
  "readmeFilename": "README.md",
  "bugs": {
    "url": "https://github.com/carlos8f/node-middler/issues"
  },
  "_id": "middler@0.7.1",
  "_from": "middler@"
}
