Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

request.onData is not getting triggered #25

Open
KiranRamashetty opened this issue Feb 19, 2019 · 2 comments
Open

request.onData is not getting triggered #25

KiranRamashetty opened this issue Feb 19, 2019 · 2 comments

Comments

@KiranRamashetty
Copy link

In request Construction the ondata is being to empty function

  this.ondata = noop
  this.onend = noop

In server.js

parser[HTTPParser.kOnBody] = onbody

function onbody (body, start, end) {
     req.ondata(body, start, end)
}

So wondering if I am missing something here? How to do I receive POST data?

@lucagez
Copy link

lucagez commented Mar 22, 2019

Hi, I am pretty late but I will add it anyway.
Here is a snippet for reading the body of a request.

const _body = req => new Promise(resolve => {
  const parts = [];
  req.ondata = (buffer, start, length) => {
    const part = buffer.slice(start, length + start).toString();
    parts.push(part);
  };

  req.onend = () => resolve(parts.join(''));
});

Then you can use it in your server like that:

const http = require('turbo-http');
const server = http.createServer().listen(3000);

server.on('request', async (req, res) => {
  const body = await _body(req);
  res.end();
});

@dalisoft
Copy link

dalisoft commented May 9, 2019

@lucagez You saved my hours. @mafintosh Maybe we should add this snippet to documentation?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants