Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple middlewares support #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
})
})
})