How to send the value of a variable from Node.js to the browser? -
i don't understand why can't value of "i" in browser. got erreur 101 (net::err_connection_reset)
var http = require('http'); var i=0; http.createserver(function (req, res) { i++; res.writeheader(200, {'content-type': 'text/plain'}); res.write(i); res.end(); }).listen(80, "127.0.0.1");
but work if :
res.write("i =" + i);
thank you
short answer:
because type of i
number.
long answer:
take @ socket.prototype.write
definition:
socket.prototype.write = function(chunk, encoding, cb) { if (typeof chunk !== 'string' && !buffer.isbuffer(chunk)) throw new typeerror('invalid data'); return stream.duplex.prototype.write.apply(this, arguments); };