Skip to content

Commit

Permalink
Add multiple middlewares support
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegomez committed Jan 9, 2017
1 parent e9adf00 commit b9bd25e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
31 changes: 30 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# koa-mount

Mount other Koa applications as middleware. The `path` passed to `mount()` is stripped
from the URL temporarily until the stack unwinds. This is useful for creating entire
from the URL temporarily until the stack unwinds. This is useful for creating entire
apps or middleware that will function correctly regardless of which path segment(s)
they should operate on.

Expand Down Expand Up @@ -96,6 +96,35 @@ app.listen(3000);
console.log('listening on port 3000');
```

You can also mount multiple middlewares if needed. It can be useful when you want to have an endpoint secured with the same app instance.

```js
var mount = require('koa-mount');
var koa = require('koa');

function *isAuthed(next){
// apply logic here
}

function *hello(next){
yield next;
this.body = 'Hello';
}

function *world(next){
yield next;
this.body = '*Secured* World';
}

var app = new koa();

app.use(mount('/hello', hello));
app.use(mount('/secured', [isAuthed, world]));

app.listen(3000);
console.log('listening on port 3000');
```

### Optional Paths

The path argument is optional, defaulting to "/":
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ module.exports = mount;
* @api public
*/

function mount(prefix, app) {
function mount(prefix, _app) {
var app = Array.isArray(_app)
? compose(_app)
: _app;

if ('string' != typeof prefix) {
app = prefix;
prefix = '/';
Expand Down
56 changes: 55 additions & 1 deletion test/mounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var request = require('supertest');
var mount = require('..');
var koa = require('koa');
var should = require('should');

describe('mount(app)', function(){
it('should mount at /', function(done){
Expand Down Expand Up @@ -237,6 +238,59 @@ describe('mount(path, app)', function(){
});
})
})

describe('when multiple middlewares are passed', function() {
var app = new koa();

var say = text => function *(next) {
this.body = text;
yield next;
};

var setStatus = status => function *() {
this.status = status;
};

app.use(mount('/hello', [say('hello'), setStatus(201)]));
app.use(mount('/world', [say('world'), setStatus(200)]));
app.use(mount('/prefix', function *() {
this.status = 204;
}));

var server = app.listen();

it('should match /prefix', function(done) {
request(server)
.get('/prefix')
.expect(204, done)
})

it('should match /hello', function(done){
request(server)
.get('/hello')
.end(function (err, res) {
should.not.exists(err);
should.exists(res);
res.statusCode.should.be.equal(201);
should.exists(res.text);
res.text.should.be.equal('hello');
done();
})
})

it('should match /world', function(done){
request(server)
.get('/world')
.end(function (err, res) {
should.not.exists(err);
should.exists(res);
res.statusCode.should.be.equal(200);
should.exists(res.text);
res.text.should.be.equal('world');
done();
})
})
})
})

describe('mount(/prefix)', function(){
Expand Down Expand Up @@ -317,4 +371,4 @@ describe('mount(/prefix/)', function(){
.get('/prefix/lkjasdf')
.expect(204, done);
})
})
})

0 comments on commit b9bd25e

Please sign in to comment.