Helidium / Mitol

Lightweight NodeJS http server
Other
175 stars 5 forks source link

req.on('data'...) does not seem to work #6

Closed udivankin closed 6 years ago

udivankin commented 7 years ago

Tried to make "hello world" post/put request handler but it does not seem to work (while GET does work as expected)

Here's the code:

const http = require('mitol');

let server = http.createServer((req, res) => {
  if (req.method === 'GET') {
    res.end('Hello World!'); // works as expected
    return;
  }

  var str = '';
  req.on('data', function(data){
    str += data;
  });
  req.on('end', function(){
    res.end('POST pingback', str);  // never fires
  });
});

server.listen(8099, () => {
    console.log('Example app listening on port 8099!')
});
Helidium commented 7 years ago

Ok, I fixed the bug. Currently the data received is the arraybuffer. Also, do note that res.end should look like this: res.end('POST pingback' + str);

udivankin commented 7 years ago

Yep, just a typo.

FYI considering you state that Mitol aimed as http drop-in replacement, res.on('data') callback argument should not include headers like it does currently, but request body only

POST /HTTP/1.1
host localhost:8081
connection keep-alive
content-length 11
origin chrome-extension://aahelpfcopefplnmnphdochceilphfeb
user-agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 YaBrowser/17.7.1.720 Yowser/2.5 Safari/537.36
content-type text/plain;charset=UTF-8
accept */*
accept-encoding gzip, deflate, br
accept-language ru,en;q=0.8

requestbody

Forgot to say thanks for your work!

Helidium commented 7 years ago

Yes, you are correct!

I will fix it...

Thank you for appreciating the project!

heroic commented 6 years ago

Whats the status of this issue? It's not letting me use koa or express with body content

Helidium commented 6 years ago

@heroic I will open a new issue, the problem is the response is the plain object atm., not the stream.

Helidium commented 6 years ago

@udivankin req.on is now implemented, also the data returned is of type string. Currently request content-type is not checked!