Difference Between "Process.Stdout.Write" and "Console.Log" in Node.Js

Difference between process.stdout.write and console.log in node.js?

console.log() calls process.stdout.write with formatted output. See format() in console.js for the implementation.

Currently (v0.10.ish):

Console.prototype.log = function() {
this._stdout.write(util.format.apply(this, arguments) + '\n');
};

A way to output logs without using process.stdout.write in Node JS?

Looks like debug module on npm writes to stderr instead:

By default debug will log to stderr

/**
* Invokes `util.format()` with the specified arguments and writes to stderr.
*/

function log(...args) {
return process.stderr.write(util.format(...args) + '\n');
}

What's the difference between console.log and response.write?

console.log puts the data on the standard output (file descriptor number 1).

response.write write the data on the response object, whatever it is. In your case it seems to be a response to an HTTP request, but it may be any other stream (including standard output).

The results of both of those commands can end up in the same place - like your screen - but taking a different path to the same destination: console.log doesn't use the network in the process but response.write (most likely) does. I say "most likely" because it is not clear from your question what this response is a response to.

Process.stdout.write appends true to the console output

I think you need to take another look at your code. some of the common advice for programming forums is to get an example simplified enough that your problem still occurs.

In this case if I write process.stdout.write('hello') to a file, and then ask node to run that file, I get the correct response of hello back, the word true doesn't show up.

According to the options from node, using -p instead of -e means you're asking it to print the result of that function call, which was a success.

  -e, --eval script          evaluate script
-p, --print evaluate script and print result

For now try the -e flag with your code.



Related Topics



Leave a reply



Submit