javascript - Need to Understand the flow of ReadWrite in NodeJS -


i trying code on nodejs, new nodejs. have write following block of code.

 var fs = require('fs'),     os = require('os');  var filename = 'server.ini'; var serverdata = os.hostname() + "\n" + os.platform() + "\n" + os.type() + "\n";  fs.existssync(filename, function(exists) {     if(exists) {         console.log("1. " + filename + " file found. server needs updated.")          fs.unlinksync(filename, function(error) {             if(error) throw error;             console.log("2. " + filename + " been unlinked server.");         });      } else {         console.log("1. " + filename + " not found.");         console.log("2. server needs configured.");     } });  fs.opensync(filename, "w+", function(error) {     if(error) throw error;     console.log("3. " + filename + " file been locked."); });   fs.writefilesync(filename, serverdata, function(error) {     if(error) throw error;     console.log("4. " + filename + " updated.");      fs.readfilesync(filename, 'utf-8', function(error, data) {         if(error) throw error;          console.log("5. reading " + filename + " file");         console.log("6. " + filename + " contents below\n");         console.log(data);         console.log("-------the end of file-------");     }); }); 

i have edited code , added sync giving me following error:

d:\nodejs\fs>node eg5.js

buffer.js:382       throw new error('unknown encoding');             ^ error: unknown encoding     @ buffer.write (buffer.js:382:13)     @ new buffer (buffer.js:261:26)     @ object.fs.writefilesync (fs.js:758:12)     @ object.<anonymous> (d:\nodejs\fs\eg5.js:28:4)     @ module._compile (module.js:449:26)     @ object.module._extensions..js (module.js:467:10)     @ module.load (module.js:356:32)     @ function.module._load (module.js:312:12)     @ module.runmain (module.js:492:10)     @ process.startup.processnexttick.process._tickcallback (node.js:244:9)  d:\nodejs\fs> 

is there wrong in code regarding utf8 !

you're calling asynchronous versions of fs object , passing callbacks.

synchronous versions exist. example, see : check synchronously if file/directory exists in node.js

in original code before editing sync:

    fs.writefile(filename, serverdata, function(error) {         ...     });      fs.readfile(filename, "utf-8", function(error, data) { 

the second argument call callback. asynchronously run exists check , call function passed in when done. fs.readfile called before writefile finishes.

if you're goal write node async code, nest read call inside write callback. if you're goal write straightforward synchronous script switch synchronous calls , write flat code.

if you're writing server code node.js, it's critical use async i/o calls since it's 1 thread on loop. if you're writing script above , want simple in synchronous order, use sync versions of api.

since node should async, sync calls exception convention end function name sync. functions fs.existssync


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -