Saturday, February 22, 2014

Reading from the User and Writing to a File in Node.js (part 2)

// I get different output when I embed! Thank you Micah!

readline = require('readline');

var fs = require('fs');

var answer = 'this answer';

var rl = readline.createInterface({

  input: process.stdin,

  output: process.stdout,

});

rl.question("What do you think of node.js?", function(answer) {

  // TODO: Log the answer in a database
  // Hmm ... I wonder what happens if I write to the file here...

  fs.writeFile('read_write_node_embed.txt', answer, function(err) {
 
    if (err) throw err;
  
    console.log('It\'s saved!');

});

// The end of the writing to the file

  console.log("Thank you for your valuable feedback:", answer);

  rl.close();

})

fs.writeFile('read_write_node_not-embed.txt', answer , function(err) {

  if (err) throw err;

  console.log('It\'s saved!');

});

No comments:

Post a Comment