Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js: In-regards to the use of the function fs.writeFile() in Node.js
    primarykey
    data
    text
    <pre><code>fs.writeFile('message.txt', 'Hello Node', function (err) { if (err) throw err; console.log('It\'s saved!'); }); </code></pre> <p>I have a question regarding the use of the function <code>fs.writeFile()</code> in Node, and the fact that it prints strings into a textfile. </p> <p>At the moment, I have a piece of code that prints the following into console. Instead of the console, I would like to modify the code a bit so it would print into a textfile. </p> <pre><code>var options = { host: 'www.google.com', path: '/' }; http.get(options, function(response) { console.log("Status Code: " + response.statusCode); for(var item in response.headers) { console.log(item + ": " + response.headers[item]); } </code></pre> <p>I would like to replace <code>console.log</code> with <code>fs.writeFile</code>, however, the <code>Hello Node</code> part of the input in <code>fs.writeFile()</code> is a string. Thus, I am stuck on this part. </p> <p>The documentation for fs.writeFile() for Node.js is here: <a href="http://nodejs.org/api/fs.html" rel="nofollow">http://nodejs.org/api/fs.html</a> </p> <p>As explained in the documentation, this is how the fs.writeFile function works <code>fs.writeFile(filename, data, [encoding], [callback])</code>. My question is in regards to the <code>data</code> input. </p> <p>I am fairly new to Node.js, therefore I would like to get some feedback on this, and hopefully work the solution out myself with some pointers. </p> <p>EDIT 1: Do I use String() which then turns my function into a string? I don't think this is right, but it could be a possibility.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload