Node.js: printing to console without a trailing newline?

后端 未结 8 2230
-上瘾入骨i
-上瘾入骨i 2020-11-28 00:27

Is there a method for printing to the console without a trailing newline? The console object documentation doesn\'t say anything regarding that:

8条回答
  •  猫巷女王i
    2020-11-28 00:49

    util.print can be used also. Read: http://nodejs.org/api/util.html#util_util_print

    util.print([...])# A synchronous output function. Will block the process, cast each argument to a string then output to stdout. Does not place newlines after each argument.

    An example:

    // get total length
    var len = parseInt(response.headers['content-length'], 10);
    var cur = 0;
    
    // handle the response
    response.on('data', function(chunk) {
      cur += chunk.length;
      util.print("Downloading " + (100.0 * cur / len).toFixed(2) + "% " + cur + " bytes\r");
    });
    

提交回复
热议问题