I am just trying some code on NodeJS, i am new to NodeJS. I 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 to be updated.")
fs.unlinkSync(filename, function(error) {
if(error) throw error;
console.log("2. " + filename + " is been unlinked from server.");
});
} else {
console.log("1. " + filename + " not found.");
console.log("2. Server needs to be configured.");
}
});
fs.openSync(filename, "w+", function(error) {
if(error) throw error;
console.log("3. " + filename + " file is been locked.");
});
fs.writeFileSync(filename, serverData, function(error) {
if(error) throw error;
console.log("4. " + filename + " is now updated.");
fs.readFileSync(filename, 'utf-8', function(error, data) {
if(error) throw error;
console.log("5. Reading " + filename + " file");
console.log("6. " + filename + " contents are below\n");
console.log(data);
console.log("-------THE END OF FILE-------");
});
});
I have edited the code and added sync but now its giving me following error:
D:\NodeJS\fs>node eg5.js
buffer.js:382
throw new Error('Unknown encoding');
^
Error: Unknown encoding
at Buffer.write (buffer.js:382:13)
at new Buffer (buffer.js:261:26)
at Object.fs.writeFileSync (fs.js:758:12)
at Object.<anonymous> (D:\NodeJS\fs\eg5.js:28:4)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
D:\NodeJS\fs>
Is there anything wrong in my code regarding utf8 !
In this guide, you can learn how to enable or disable the Node. js driver's UTF-8 validation feature. UTF-8 is a character encoding specification that ensures compatibility and consistent presentation across most operating systems, applications, and language character sets.
The setEncoding function in Node. js alters the encoding of character data stored inside a readable stream. It comes as part of the stream module. By changing character encodings, programmers can use the setEncoding method to interpret the same piece of data in multiple ways.
The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.
readFileSync
doesn't take a callback. I guess you wanted to use readFile
instead.
You have the same problem for writeFileSync
. Those callbacks called upon completion make no sense when using IO functions synchronously. It's better to use the asynchronous functions (without the "Sync"), pay attention to the fact they take different arguments.
And the documentation always mentions "utf8"
, not "utf-8"
, I don't know if the latter is supported.
According to the node.js API documentation, writeFileSync takes 3 arguments:
Nowhere does it specify a callback. Only the async version takes a callback.
http://www.nodejs.org/api/fs.html#fs_fs_writefilesync_filename_data_options
Try this instead of your writeFileSync block:
fs.writeFileSync(filename, serverData, { encoding: 'utf8'});
console.log("4. " + filename + " is now updated.");
var contents = fs.readFileSync(filename, 'utf8');
console.log("5. Reading " + filename + " file");
console.log("6. " + filename + " contents are below\n");
console.log(contents);
console.log("-------THE END OF FILE-------");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With