inversify-restify-utils 控制器中的空上下文和主体
inversify-restify-utils an empty context and a body in the controller
我试着开始工作 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();
});
有什么建议吗?
我错误配置了服务器。我需要在创建应用程序时添加 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();
配置示例。
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
}));
}
我试着开始工作 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();
});
有什么建议吗?
我错误配置了服务器。我需要在创建应用程序时添加 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();
配置示例。
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
}));
}