1
我试图处理我的koa路由器中的POST请求。不幸的是,每次我尝试使用我的表单发送数据时,我什么也得不到。我试过koa-bodyparser,没有运气。我使用Jade作为模板引擎。Koa路由器和POST
router.js:
var jade = require('jade');
var router = require('koa-router')();
var bodyParser = require('koa-bodyparser');
exports.enableRouting = function(app){
app.use(bodyParser())
router.get('/game/questions', function *(next){
this.status = 200;
this.body = jade.renderFile('game_questions.jade');
});
router.post('/game/questions', function *(next){
console.log(this.request.body);
this.status = 200;
this.body = jade.renderFile('game_questions.jade');
});
app
.use(router.routes())
.use(router.allowedMethods());
}
和game_questions.jade的一部分:
form(method='post' id='New_Question_Form')
input(type='text', id='New_Question_Text')
input(type='submit' value='Add Question')
this.request.body
是空的,this.request
回报:方法,URL和报头。任何帮助感谢!
尝试onerror选项https://github.com/koajs/bodyparser#options – monkey
试过了 - 没有错误。谢谢:) – jwitos
没有解决问题。尽管设法通过使用Socket.io来保存表单而不是使用POST发送来解决它。如果有人很好奇[这是github上的diff](https://github.com/jwitos/takismieszny/commit/a53e44b80ce474bcc30cda97cb8222ffeb666b51)我是怎么做到的。 – jwitos