0

I don't understand why I can't get the value of "i" in the browser. I got this 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 it does work if :

res.write("i =" + i);

Thank you

1 Answer 1

2

Short Answer:

Because the type of i is number.

Long Answer:

Take a look at 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);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.