简体   繁体   English

如何将 append 换行到 Node.js

[英]How to append to New Line in Node.js

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.我正在尝试使用 Node.js 将 Append 数据写入日志文件,这工作正常,但不会转到下一行。 \n doesn't seem to be working in my function below. \n似乎在我下面的 function 中不起作用。 Any suggestions?有什么建议么?

function processInput ( text ) 
{     
  fs.open('H://log.txt', 'a', 666, function( e, id ) {
   fs.write( id, text + "\n", null, 'utf8', function(){
    fs.close(id, function(){
     console.log('file is updated');
    });
   });
  });
 }

It looks like you're running this on Windows (given your H://log.txt file path).看起来您正在 Windows 上运行它(给定您的H://log.txt文件路径)。

Try using \r\n instead of just \n .尝试使用\r\n而不仅仅是\n

Honestly, \n is fine;老实说, \n很好; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines.您可能正在记事本或其他不呈现非 Windows 换行符的文件中查看日志文件。 Try opening it in a different viewer/editor (eg Wordpad).尝试在不同的查看器/编辑器(例如写字板)中打开它。

Use the os.EOL constant instead.请改用 os.EOL 常量。

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');
    });
   });
  });
 }

use \r\n combination to append a new line in node js使用\r\n组合在节点 js 中追加新行

  var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
  stream.once('open', function(fd) {
    stream.write(msg+"\r\n");
  });

Alternatively, you can use fs.appendFile method或者,您可以使用fs.appendFile方法

let content = 'some text';
content += "\n";
fs.appendFile("helloworld.txt", content, (err) => {
    return console.log(err);
});
const { writeFileSync } = require('fs'); writeFileSync( './content/result-sync.txt', `\n Here is the result : ${first}, ${second}`, { flag: 'a' } )

Try:尝试:

var fs =require('fs');

const details=require('./common');
var info=JSON.stringify(details);

const data=fs.writeFileSync('./tmp/hello/f1.txt',`\n${info}`,{'flag':'a'},function(err,data){
    
if(err) return console.error("error",error);
    console.log(data);

});

//steps to exceute 1.Install all the required modules(ie fs is required here). //执行步骤 1.安装所有需要的模块(即这里需要fs)。 2.Here (.common) files has json object which i was importing from another file. 2.这里(.common)文件有 json object 我是从另一个文件导入的。 3.then import the file and store in details variable. 3.然后导入文件并存储在details变量中。 4.While performing operations json data has to be converted into string format (so JSON.stringify). 4.执行操作时json数据必须转换成字符串格式(所以JSON.stringify)。 5.WriteFileSync (its an synchronous function) 6.once function execution is completed response is returned. 5.WriteFileSync(它是一个同步函数) 6.once function 执行完成返回响应。 7.store response in data variable and print in console.log 7.将响应存储在数据变量中并在控制台日志中打印

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM