javascript - Request body is undefined for POST request in KOA -


i beginner in nodejs. trying create http server uses koa framework handling http requests. following code server.

app.use(function * (next){     var ctx = this;      var resource = url.parse(ctx.request.url, true, true).pathname;     switch(resource) {             case '/':                     resource = config.app.root + '/dist/index.html';                     ctx.type = mime.lookup(resource);                     ctx.body = fs.readfilesync(resource, 'utf-8');                     ctx.response.set('access-control-allow-origin', '*');                     break;             case '/fudge/contact':                     console.log('============================');                     console.log(ctx.request); // no body                     console.log('============================');                     console.log(ctx.req);     // no body                     console.log('============================');                     console.log(ctx.request.body) // undefined                     break;             default:                     resource = config.app.root + resource;                     ctx.type = mime.lookup(resource);                     ctx.body = fs.readfilesync(resource, 'utf-8');                     ctx.response.set('access-control-allow-origin', '*');                     break;     }}); 

as mentioned in case '/fudge/contact' ctx.request.body undefined. but, when check ctx.request or ctx.req, shows content-length 98(or non-zero).

following output get:

============================  {  method: 'post',   url: '/fudge/contact',   header:     { host: 'localhost:9090',      'user-agent': 'mozilla/5.0 (x11; ubuntu; linux x86_64; rv:36.0) gecko/20100101 firefox/36.0',      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      'accept-language': 'en-us,en;q=0.5',      'accept-encoding': 'gzip, deflate',      'content-type': 'text/plain; charset=utf-8',      referer: 'http://localhost:9090/',      'content-length': '98',      connection: 'keep-alive',      pragma: 'no-cache',      'cache-control': 'no-cache'}} ============================ { _readablestate:     { objectmode: false,      highwatermark: 16384,      buffer: [],      length: 0, ... more lines no body. ============================ undefined 

following client code: have used httpclient library of aurelia framework.

        contactus(){             this.http.createrequest('/fudge/contact')                     .aspost()                     .withbaseuri('http://localhost:9090')                     .withheader('content-type','text/plain')                     .withcontent('{"heading":this.heading, "firstname":this.firstname, "lastname":this.lastname, "query":this.query}')                     .send();             alert(`thank you, ${this.fullname}, query has been submitted`);      } 

the above code javascript - es7 , valid using transpilers babel. point able send post request server successfully, there no body in request. please suggest solution.

versions used: node v0.12.0, koa v0.19.1

koa doesn't parse request body default, need add middleware body parsing, koa-body example:

var app     = require('koa')(),     router  = require('koa-router'),     koabody = require('koa-body')();  app.use(router());  app.post('/users', koabody,   function *(next) {     console.log(this.request.body);     // => post body     this.body = json.stringify(this.request.body);   } ); app.listen(3131) console.log('curl -i http://localhost:3131/ -d "name=test"'); 

koa's philosophy have little possible in core framework, , let people compound system assembling specialized middlewares. have seen, there few different packages parsing request's body, simpler , others have more features/options (e.g. koa-better-body). it's giving choices developer without creating big monolith.

while approach may seem strange @ first, it: allows me choose features need without bloating framework options every possible use case.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -