node.js - Giving encoding error 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 !

readfilesync doesn't take callback. guess wanted use readfile instead.

you have same problem writefilesync. callbacks called upon completion make no sense when using io functions synchronously. it's better use asynchronous functions (without "sync"), pay attention fact take different arguments.

and documentation mentions "utf8", not "utf-8", don't know if latter supported.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -