Comparing version 1.0.0 to 1.1.0
40
index.js
@@ -6,2 +6,3 @@ | ||
var debug = require('debug')('koa-mount'); | ||
var compose = require('koa-compose'); | ||
@@ -16,6 +17,8 @@ | ||
/** | ||
* Mount `app` to `path`. | ||
* Mount `app` to `path`, `app` | ||
* may be a Koa application or | ||
* middleware function. | ||
* | ||
* @param {String} path | ||
* @param {Application} app | ||
* @param {String|Application|Function} path, app, or function | ||
* @param {Application|Function} [app or function] | ||
* @return {Function} | ||
@@ -26,4 +29,14 @@ * @api public | ||
function mount(path, app) { | ||
if ('string' != typeof path) { | ||
app = path; | ||
path = '/'; | ||
} | ||
var name = app.name || 'unnamed'; | ||
debug('mount %s %s', path, name); | ||
return function(upstream){ | ||
var downstream = compose(app.middleware)(upstream); | ||
var downstream = app.middleware | ||
? compose(app.middleware)(upstream) | ||
: app(upstream); | ||
@@ -37,4 +50,6 @@ return function *(){ | ||
// strip the path prefix | ||
this.path = this.path.replace(path, '') || '/'; | ||
this.path = replace(this.path, path); | ||
debug('enter %s -> %s', prev, this.path); | ||
yield downstream; | ||
debug('leave %s -> %s', prev, this.path); | ||
@@ -45,2 +60,17 @@ // restore prefix downstream | ||
} | ||
} | ||
/** | ||
* Replace `prefix` in `path`. | ||
* | ||
* @param {String} path | ||
* @param {String} prefix | ||
* @return {String} | ||
* @api private | ||
*/ | ||
function replace(path, prefix) { | ||
path = path.replace(prefix, '') || '/'; | ||
if ('/' != path[0]) path = '/' + path; | ||
return path; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/mount", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
@@ -17,8 +17,12 @@ "koa", | ||
"devDependencies": { | ||
"koa": "0.0.1" | ||
"koa": "0.0.1", | ||
"should": "~1.2.2", | ||
"mocha": "~1.12.1", | ||
"supertest": "~0.7.1" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"koa-compose": "~1.0.0" | ||
"koa-compose": "~1.0.0", | ||
"debug": "*" | ||
} | ||
} |
@@ -9,5 +9,21 @@ | ||
## Installation | ||
```js | ||
$ npm install koa-mount | ||
``` | ||
var mount = require('./'); | ||
## Examples | ||
View the [./examples](blob/master/examples) directory for working examples. | ||
### Mounting Applications | ||
Entire applications mounted at specific paths. For example you could mount | ||
a blog application at "/blog", with a router that matches paths such as | ||
"GET /", "GET /posts", and will behave properly for "GET /blog/posts" etc | ||
when mounted. | ||
```js | ||
var mount = require('koa-mount'); | ||
var koa = require('koa'); | ||
@@ -61,10 +77,62 @@ | ||
## Installation | ||
### Mounting Middleware | ||
Mount middleware at specific paths, allowing them to operate independently | ||
of the prefix, as they're not aware of it. | ||
```js | ||
$ npm install koa-mount | ||
var mount = require('koa-mount'); | ||
var koa = require('koa'); | ||
function hello(next){ | ||
return function *(){ | ||
yield next; | ||
this.body = 'Hello'; | ||
} | ||
} | ||
function world(next){ | ||
return function *(){ | ||
yield next; | ||
this.body = 'World'; | ||
} | ||
} | ||
var app = koa(); | ||
app.use(mount('/hello', hello)); | ||
app.use(mount('/world', world)); | ||
app.listen(3000); | ||
console.log('listening on port 3000'); | ||
``` | ||
### Optional Paths | ||
The path argument is optional, defaulting to "/": | ||
```js | ||
app.use(mount(a)); | ||
app.use(mount(b)); | ||
``` | ||
## Debugging | ||
Use the __DEBUG__ environement variable to whitelist | ||
koa-mount debug output: | ||
``` | ||
$ DEBUG=koa-mount node myapp.js & | ||
$ GET /foo/bar/baz | ||
koa-mount enter /foo/bar/baz -> /bar/baz +2s | ||
koa-mount enter /bar/baz -> /baz +0ms | ||
koa-mount enter /baz -> / +0ms | ||
koa-mount leave /baz -> / +1ms | ||
koa-mount leave /bar/baz -> /baz +0ms | ||
koa-mount leave /foo/bar/baz -> /bar/baz +0ms | ||
``` | ||
## License | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4210
56
136
2
4
1
+ Addeddebug@*
+ Addeddebug@4.4.0(transitive)
+ Addedms@2.1.3(transitive)