skip to Main Content

I’m working on a Node.js application and need to append newline characters to a file in a way that is valid and recognized by various text editors and tools(Specific for .bib files).

I tried to add n after each field. This is one case.

const data = 'This is some text that I want to append with a new line';
const newline = 'n';

res.setHeader('Content-Type', 'text/x-bibtex;charset=utf-8');
res.setHeader('Content-Disposition', `attachment;filename=${filename}`);
res.send(data);

However, when I open the file in a text editor, the newline character doesn’t seem to be recognized, and everything appears on a single line.

2

Answers


  1. Chosen as BEST ANSWER

    I was doing some sort of

    const data = 'This is some text that I want to append with a new line'.trim().replace();
    

    So I just add '/n/ after replace method :

    const data = 'This is some text that I want to append with a new line'.trim().replace()+ '/n';
    

  2. You can use the built in os module.
    Its valid and recognized by various text editors and tools, you need to use the appropriate newline sequence based on the operating system’s conventions

    const os = require('os');
    const newline = os.EOL;
    fs.appendFileSync(filePath, `${data}${newline}`, { encoding: 'utf8' });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search