bearcatjs / bearcat

powered by enhanced javaScript objects
http://bearcatjs.org/
Other
591 stars 110 forks source link

koa with bearcat not work #185

Closed zj8487 closed 9 years ago

zj8487 commented 9 years ago

hi, i plan to migrate to koa from express.

i write a middleware with bearcat as follow:

bearcat.getRoute("testMiddleware", "auth") //for route
"use strict";

var TestMiddleware = function() {
    this.$id = "testMiddleware";
}

TestMiddleware.prototype.auth =  function* (next) {
    this.checkBody('password', 'Invalid password').notEmpty();
    if (this.errors){
        this.body = this.errors;
        return;
    }
};

module.exports = TestMiddleware;

but this.body is not defined. i print the this in the auth function. it says that this is TestMiddleware object.

zj8487 commented 9 years ago

@fantasyni i think a todo example based on koa is helpful!

fantasyni commented 9 years ago

I will test it soon ~

zj8487 commented 9 years ago

this style will work.

http.post('/xx/auth/', 
    testMiddleware.auth(),
    );
"use strict";

var TestMiddleware = function() {
    this.$id = "testMiddleware";
}

TestMiddleware.prototype.auth =  function () {
    return function* (next) {
        this.checkBody('password', 'Invalid password').notEmpty();
        if (this.errors){
            this.body = this.errors;
            return;
        }
    }
};

module.exports = TestMiddleware;