Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "stacked", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": "Felix Gnass <fgnass@gmail.com>", | ||
"description": "bundle multiple middleware functions into one stack", | ||
"scripts": { | ||
"test": "mocha test/ --reporter spec" | ||
}, | ||
"keywords": [ | ||
@@ -16,3 +19,9 @@ "connect", | ||
"url": "http://github.com/fgnass/stacked.git" | ||
}, | ||
"devDependencies": { | ||
"connect": "^3.0.1", | ||
"supertest": "^0.13.0", | ||
"chai": "^1.9.1", | ||
"mocha": "^1.20.1" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/fgnass/stacked.svg)](https://travis-ci.org/fgnass/stacked) | ||
# stacked | ||
@@ -9,4 +11,10 @@ | ||
## Usage | ||
### Installation | ||
``` | ||
npm install stacked --save | ||
``` | ||
### Usage | ||
```javascript | ||
@@ -36,1 +44,30 @@ var stacked = require('stacked') | ||
``` | ||
### Run Tests | ||
``` | ||
npm install | ||
npm test | ||
``` | ||
## The MIT License (MIT) | ||
Copyright (c) 2014 Felix Gnass | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
var URL = require('url') | ||
module.exports = function(/* fn1, fn2, ... */) { | ||
var layers = Array.prototype.slice.call(arguments) | ||
function handle(req, res, out) { | ||
var handle = function (req, res, out) { | ||
var i = 0 | ||
function next(err) { | ||
var layer = layers[i++] | ||
var layer = handle.layers[i++] | ||
@@ -29,10 +28,15 @@ if (!layer || res.headerSent) { | ||
} | ||
handle.layers = Array.prototype.slice.call(arguments) | ||
handle.use = function(fn) { | ||
if (typeof fn == 'object' && fn.handle) fn = fn.handle.bind(fn) | ||
layers.push(fn) | ||
handle.layers.push(fn) | ||
return this | ||
} | ||
handle.mount = function(path, fn) { | ||
return this.use(sub(path, fn)) | ||
} | ||
return handle | ||
@@ -42,3 +46,2 @@ } | ||
function sub(mount, fn) { | ||
if (mount.substr(-1) != '/') mount += '/' | ||
@@ -51,3 +54,3 @@ if (typeof fn == 'object' && fn.handle) fn = fn.handle.bind(fn) | ||
if (url.substr(0, mount.length) !== mount) return next() | ||
if (url.substr(0, mount.length) !== mount && url.substr(0, mount.length) + '/' !== mount) return next() | ||
@@ -54,0 +57,0 @@ // modify the URL |
7736
7
147
72
4