flatiron / director

a tiny and isomorphic URL router for JavaScript

Home Page:http://github.com/flatiron/director

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Undefined POST body

opened this issue · comments

using director 1.2.8
node 6.3.0

I am having an issue with POST request using director. The req.body object is empty. Below is my test code.

  const data = querystring.stringify({
  ItemType: '1', ID: '7620344',
  ManufacturingStationID: '117',
});

const OPTIONS = {
  host: 'localhost', 
  port: '3030'
  path: '/pws/reportError',
  method: 'POST',
  protocol: 'http:',
  headers: {
    'Content-Type': 'application/json',
  },
};

const postReq = http.request(OPTIONS, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          console.log('Response :) ' + chunk);
      });
});
postReq.write('{"msg": "Hello, World"}');
postReq.end();

This is the server endpoint function

function reportError () {
  this.req.on('data', function (chunk) {
    console.log('chunk' + chunk);
  });

  console.log(JSON.stringify(this.req.body))

  this.res.writeHead(200, {});
  this.res.end('done with reportError');
}

this.req.body is an empty object. I would expect it to be able to do this.req.body.msg to see 'Hello, World', instead I get undefined. Also this.erq.on('data', ... does not fire.

I have other GET endpoints working fine, and I can confirm that it is hitting the reportError post endpoint, but this.req.body is empty?

I am running a simple server and can confirm that req does have cought before director does have the expected data.

const server = http.createServer(function (req, res) {
  if(req.url === '/pws/reportError' && req.method == 'POST'){
    req.on('data', function (chunk) {
      console.log('chunk' + chunk);
    });
  }
  router.dispatch(req, res, (err) => {
    if (err) {
      res.writeHead(404);
      res.end('some problem from the server');
    }
  });
});

The result of console.log('chunk' + chunk); is the expected data, while this does not happen for the endpoint routed to by director.

Found my error, fault with me not director.