Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
haoxin committed Nov 19, 2015
1 parent 3187fcc commit 8dd23e9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
31 changes: 17 additions & 14 deletions examples/cascade.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,29 @@
*/

var mount = require('..');
var koa = require('koa');
var Koa = require('koa');

var app = koa();
var a = koa();
var b = koa();
var c = koa();
var app = new Koa();
var a = new Koa();
var b = new Koa();
var c = new Koa();

a.use(function *(next){
yield next;
if (!this.body) this.body = 'foo';
a.use((ctx, next) => {
return next().then(() => {
if (!ctx.body) ctx.body = 'foo';
});
});

b.use(function *(next){
yield next;
if (!this.body) this.body = 'bar';
b.use((ctx, next) => {
return next().then(() => {
if (!ctx.body) ctx.body = 'bar';
});
});

c.use(function *(next){
yield next;
this.body = 'baz';
c.use((ctx, next) => {
return next().then(() => {
ctx.body = 'baz';
});
});

app.use(mount('/foo', a));
Expand Down
21 changes: 11 additions & 10 deletions examples/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
*/

var mount = require('..');
var koa = require('koa');
var Koa = require('koa');

var a = koa();
var b = koa();
var c = koa();
var a = new Koa();
var b = new Koa();
var c = new Koa();

a.use(function *(next){
yield next;
a.use((ctx, next) => {
return next();
});

b.use(function *(next){
yield next;
b.use((ctx, next) => {
return next();
});

c.use(function *(next){
yield next;
c.use((ctx, next) => {
return next().then(() => {
throw new Error('tobi escaped!');
});
});

a.use(mount(b));
Expand Down
18 changes: 10 additions & 8 deletions examples/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
*/

var mount = require('..');
var koa = require('koa');
var Koa = require('koa');

function *hello(next){
yield next;
this.body = 'Hello';
function hello(ctx, next){
return next().then(() => {
ctx.body = 'Hello';
});
}

function *world(next){
yield next;
this.body = 'World';
function world(ctx, next){
return next().then(() => {
ctx.body = 'World';
});
}

var app = koa();
var app = new Koa();

app.use(mount('/hello', hello));
app.use(mount('/world', world));
Expand Down
22 changes: 12 additions & 10 deletions examples/pathless.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
*/

var mount = require('..');
var koa = require('koa');
var Koa = require('koa');

// GET /hello

var a = koa();
var a = new Koa();

a.use(function *(next){
yield next;
if ('/hello' == this.path) this.body = 'Hello';
a.use((ctx, next) => {
return next().then(() => {
if ('/hello' == ctx.path) ctx.body = 'Hello';
});
});

// GET /world

var b = koa();
var b = new Koa();

b.use(function *(next){
yield next;
if ('/world' == this.path) this.body = 'World';
b.use((ctx, next) => {
return next().then(() => {
if ('/world' == ctx.path) ctx.body = 'World';
});
});

// app

var app = koa();
var app = new Koa();

app.use(mount(a));
app.use(mount(b));
Expand Down
30 changes: 15 additions & 15 deletions examples/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
* GET /world
*/

var mount = require('./');
var koa = require('koa');
var mount = require('../');
var Koa = require('koa');

// hello

var a = koa();
var a = new Koa();

a.use(function *(next){
yield next;
this.body = 'Hello';
}
);
a.use((ctx, next) => {
return next().then(() => {
ctx.body = 'Hello';
});
});

// world

var b = koa();
var b = new Koa();

b.use(function *(next){
yield next;
this.body = 'World';
}
);
b.use((ctx, next) => {
return next().then(() => {
ctx.body = 'World';
});
});

// app

var app = koa();
var app = new Koa();

app.use(mount('/hello', a));
app.use(mount('/world', b));
Expand Down

0 comments on commit 8dd23e9

Please sign in to comment.