How to append to New Line in Node.js

后端 未结 3 1048
执念已碎
执念已碎 2020-12-02 09:57

I\'m trying to Append data to a Log file using Node.js and that is working fine but it is not going to the next line. \\n doesn\'t seem to be working in my fun

相关标签:
3条回答
  • 2020-12-02 10:07

    It looks like you're running this on Windows (given your H://log.txt file path).

    Try using \r\n instead of just \n.

    Honestly, \n is fine; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines. Try opening it in a different viewer/editor (e.g. Wordpad).

    0 讨论(0)
  • 2020-12-02 10:10

    Use the os.EOL constant instead.

    var os = require("os");
    
    function processInput ( text ) 
    {     
      fs.open('H://log.txt', 'a', 666, function( e, id ) {
       fs.write( id, text + os.EOL, null, 'utf8', function(){
        fs.close(id, function(){
         console.log('file is updated');
        });
       });
      });
     }
    
    0 讨论(0)
  • 2020-12-02 10:18

    use \r\n combination to append a new line in node js

      var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
      stream.once('open', function(fd) {
        stream.write(msg+"\r\n");
      });
    
    0 讨论(0)
提交回复
热议问题