gpt4 book ai didi

node.js - inversify-restify-utils Controller 中的空上下文和主体

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:17 25 4
gpt4 key购买 nike

我尝试开始工作inversify-restify-utils,但遇到了问题。请求上下文和主体变量为空。我的 Controller :

@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
}
}

我的服务器.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
}));
}

关于node.js - inversify-restify-utils Controller 中的空上下文和主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44132075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com