-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
请问post handle时 如何通过ctx取得post body #157
Labels
enhancement
New feature or request
Comments
临时解决方案
由于被限制直接require,但实际上可以引用相对路径。 const bodyParser = require('./node_modules/koa-bodyparser'); //relative path
// bodyparser wrap
post('/XXXX').to.handle(bodyParser()).handle((ctx) => {
console.log('ctx', ctx.request.body);
ctx.type = 'html';
ctx.body = ctx.request.body;
}); 长期解决方案请关注这个issue, 因为 并非所有请求都应该默认parse body,因为考虑到代理等情况 ,我们需要评估下,但至少会提供一个 body-parser 插件出来, 避免上述又黑又脏的操作。比如 post('/XXXX').to.bodyParser().handle(...) |
似乎更应该提供一个 action 供用户方便引入任意 koa middleware : const bodyParser = require('koa-bodyparser');
post('/XXXX').to.middleware(bodyParser).handle((ctx) => {
console.log('ctx', ctx.request.body);
ctx.type = 'html';
ctx.body = ctx.request.body;
}); |
@xuchaoying post('/(.*)').to.handle(bodyParser()); //后续所有post 操作都做bodyParser过滤
post('/XXXX').to.handle( ctx => {
ctx.body = ctx.request.body;
}); |
恩恩,我意思是需要处理用户等第三方middleware引入的便捷性问题,而不是直接提供bodyParser这样的action |
v1.1.5 版本可以支持在 route 文件中引入 koa 中间件了。 const bodyParser = require('koa-bodyparser');
post('/test/post').to.handle(bodyParser()).handle((ctx) => {
ctx.type = 'html';
ctx.body = ctx.request.body;
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ctx.request.body
为空The text was updated successfully, but these errors were encountered: