title: How do I create a line break in a JavaScript string to feed to NodeJS to write to a text file?
body: <p>I've created a simple HTML page that takes some input from the user to store on a server to be retrieved later -- this is how the text is treated when the user clicks a submit button (I've placed numbered comments under the key lines but provide the surrounding code for context):</p>
  
  <pre><code>var origText = $('#input-field').val(),
          // 1. GETS WHATEVER USER TYPED INTO INPUT FIELD
      jsonText = '{ "text": "' + origText + '" }',
          // 2. WRAPS IT AN OBJECT STRING
      ajaxText = encodeURIComponent(jsonText);
          // 3. ENCODES THE STRING FOR USE WITH AJAX
  
  $.ajax({
      url: 'http://localhost:8124/',
      data: 'save=' + ajaxText + '&amp;fn=save',
          // 4. THE ENCODED STRING IS ADDED TO THE QUERY SECTION OF THE AJAX REQUEST
      dataType: "jsonp",
      cache: false,
      timeout: 5000,
      success: function(data) {
          $("#input-ready").html(data.save.text);
      },
      error: function(jqXHR, textStatus, errorThrown) {
          alert('error ' + textStatus + " " + errorThrown);
      }
  });
  </code></pre>
  
  <p>The request is sent to a NodeJS server I am running locally, which (within the <code>http.createServer</code> callback) does the following:</p>
  
  <pre><code>var queryObj = url.parse(req.url, true).query;
      // 1. GET THE QUERY PART OF THE REQUEST/URL AS AN OBJECT
  res.writeHead(200, {'Content-Type': 'application/javascript'});
      // 2. PREPARE SERVER'S RESPONSE AS JAVASCRIPT
  queryObj.fn = queryObj.fn || '';
  queryObj.save = queryObj.save || '';
  queryObj.callback = queryObj.callback || '';
      // 3. ENSURE THE PROPERTIES NEEDED ARE DEFINED, EVEN IF FALSE-Y
  if (queryObj.fn === 'save') {
      // 4. IF THE REQUEST IS TO SAVE THEN WRITE THE USER INPUT AS OBJECT TO A TEXT FILE
      fs.writeFile('jsonp-storage-2.txt', queryObj.save, function (err) {
          if (err) {
              throw err;
          } else {
              console.log('Saved message successfully.', 'Ready to be read now.');
              res.end(queryObj.callback +
                  '({ fn: "' + queryObj.fn + '", save: ' + queryObj.save + ' })');
          }
      });
  }
  </code></pre>
  
  <p>Assuming the user types and submits "this is a line of text", the output on the server is a text file called jsonp-storage-2.txt containing this:</p>
  
  <pre><code>{ "text": "this is a line of text" }
  </code></pre>
  
  <p>After all that, my question is quite simple. How do I prettify the output in the text file?</p>
  
  <p>The code needs work but I'm thinking of using this to try storing larger objects for reading later. However, at the server end, I would like the files (for now) to be easy for me to read when opening them with Notepad, for example.</p>
  
  <p>I have tried using <code>\n</code> and <code>\t</code> like so:</p>
  
  <pre><code>jsonText = '{\n\t"text": "' + origText + '"\n}',
  </code></pre>
  
  <p>I've also tried <code>\r</code>. But the lines remain unbroken.</p>
  
  <p>Some answers suggest that I can only do this <a href="https://stackoverflow.com/questions/4768118/how-to-break-line-in-javascript">at the level of the encoded string</a> or <a href="https://stackoverflow.com/questions/6860206/how-to-format-no-line-break-json-file">after the file has been written</a>. Perhaps I missed something simple. Is there a way to do it by manipulating my <code>jsonText</code> variable?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Just to be clearer about the output desired -- currently the content of the text file produced looks like this:</p>
  
  <pre><code>{ "text": "this is a line of text" }
  </code></pre>
  
  <p>But I'd like to know if it can be produced like this:</p>
  
  <pre><code>{
      "text": "this is a line of text"
  }
  </code></pre>