Koa-router And POST
I'm trying to handle POST request within my koa-router. Unfortunately, every time I try to get data send using my form, I get nothing. I've tried koa-bodyparser, no luck there. I'm
Solution 1:
In case anyone stumbles upon this in their searches, let me suggest koa-body which may be passed to a post request like so:
var koa = require('koa');
var http = require('http');
var router = require('koa-router')();
var bodyParser = require('koa-body')();
router.post('/game/questions', bodyParser, function *(next){
console.log('\n------ post:/game/questions ------');
console.log(this.request.body);
this.status = 200;
this.body = 'some jade output for post requests';
yield(next);
});
startServerOne();
function startServerOne() {
var app = koa();
app.use(router.routes());
http.createServer(app.callback()).listen(8081);
console.log('Server 1 Port 8081');
}
but what would happen if post data was sent to /game/questions you say? Let us turn to curl in its infinite wisdom.
curl --data "param1=value1&pa//localhost:8081/game/questions'
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 34
Date: Thu, 17 Dec 2015 21:24:58 GMT
Connection: keep-alive
some jade output for post requests
And on the console of logs:
------ post:/game/questions ------
{ param1: 'value1', param2: 'value2' }
And of course, if your jade is incorrect no body parser can save you.
Post a Comment for "Koa-router And POST"