I'm trying to implement a simple HTTP endpoint for an application written in node.js. I've created the HTTP server, but now I'm stuck on reading the request content body:
http.createServer(function(r, s) {
console.log(r.method, r.url, r.headers);
console.log(r.read());
s.write("OK");
s.end();
}).listen(42646);
Request's method, URL and headers are printed correctly, but r.read()
is always NULL. I can say it's not a problem in how the request is made, because content-length
header is greater than zero on server side.
Documentation says r
is a http.IncomingMessage
object that implements the Readable Stream interface, so why it's not working?
r.on("data",function(chunk))
where you can directly work with the read data. In contrast toreadable
,data
signals that something has been read from the stream, not that something can be read. And that is the preferred way for a io anyway. – Cupellation