Search code examples
node.jsterminalconsole.logresponse.write

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


With the exception of using console.log to print what's received from a request and response.write to echo a request back, these two seem to essentially do the same thing (echo data back to me). Thanks in advance!


Solution

  • 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.