2017-05-23 17 views
0

나는 작동을 시도하지만 inversify-restify-utils 문제가 발생했습니다. 요청 컨텍스트 및 본문 변수가 비어 있습니다. 내 컨트롤러 :inversify-restify-utils 컨트롤러의 빈 컨텍스트와 본문

@Controller('/users') 
@injectable() 
export class UsersController implements interfaces.Controller { 

    @Post('/') 
    createUser(req: restify.Request, res: restify.Response, next: restify.Next) { 
    console.log(req.body); // undefined 
    } 
} 

내 server.ts :

// to get query params in req.query 
this.server.use(restify.acceptParser(this.server.acceptable)); 
// to get passed json in req.body 
this.server.use(restify.bodyParser()); 

this.server.post('/test', (req, res, next) => { 
    console.log(req.body); // ok 
    next(); 
}); 

어떤 제안이?

답변

0

서버를 잘못 구성했습니다. 앱을 생성하는 동안 setConfig()를 추가해야했습니다 :

//create restify application 
this.app = new InversifyRestifyServer(container, { 
    name: AppConstants.APP_NAME, 
    version: nconf.get("server:api_version"), 
    log: this.logger 
}).setConfig((app) => { 
    this.config(app); // configure app 
}).build(); 

config의 예.

public config(app) { 
    // configure cors 
    app.use(restify.CORS({ 
     origins: nconf.get("server:origins"), // defaults to ['*'] 
     credentials: false,     // defaults to false 
    })); 

    // to get query params in req.query 
    // this.server.use(restify.queryParser()); 
    app.use(restify.acceptParser(app.acceptable)); 
    // to get passed json in req.body 
    app.use(restify.bodyParser()); 

    setupPassport(passport); 

    app.post('/test', (req, res, next) => { 
     console.log(req.body); 
     res.json(new Response(true, 'ok')); 
     next(); 
    }); 

    // start listening 
    app.listen(this.getPort(), this.getHost(),() => { 
     log(`${app.name} listening at ${app.url}`); 
    }); 
    // error handler 
    app.on('error', (error) => { 
     this.onError(error); 
    }); 
    // process exceptions 
    app.on('uncaughtException', function (request, response, route, error) { 
     console.error(error.stack); 
     response.send(error); 
    }); 
    // audit logger 
    app.on('after', restify.auditLogger({ 
     log: this.logger 
    })); 
    }