I am trying to take an input in Japanese language and pass the value as parameter to another function to retrieve data from the function but whenever I run node app.js it doesn't recognize the input. But in the browser it is working just fine.
So it is accessing data from json const innerArray = { Name : 'Test', Prefecture: '東京都' }
While accessing it:
let prefectureName = innerArray.Prefecture
console.log(prefectureName)
The output is ???
When I use the input in english it also works. Then I also tried to convert the japanese input into english. then again the same problem remains as it can not read the input.
Can anyone help me regarding this matter?
I suspect this is only a problem in your log console. Node.js uses UTF-16 internally, so Japanese characters are fully supported.
I'd suggest trying the following:
const fs = require("fs");
const innerArray = { Name : 'Test', Prefecture: '東京都' }
fs.writeFileSync("test.json", JSON.stringify(innerArray), "utf8");
console.log("innerArray:", innerArray);
Then open test.json in something like Notepad++, you should see the characters rendered correctly.
If I try this example in Visual Studio Code, the output is fine too since the console or output font has support for Japanese Characters.
Related
What do i get when i read a text file(notepad file)? I'm guessing it returns a string after reading the text file? I want to be able to index the returned value, for example, i want to use text.indexOf() to get a specific value in the returned values
i'm using node.js
const fs = require('fs')
function readText(filepath){
var text = fs.readFileSync(filepath, 'utf8');
return text;
}
The function fs.readFileSync as you have used it will return the contents of the file as a string.
You can find the documentation for readFileSync and other fs api's here: https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options
It's often helpful to experiment with code snippets like this in a REPL, by adding console.log statements or pausing in a debugger to better understand what is happening.
first time posting here!:)
As title says I have a win reg fie (reg_sz) which contains "name" and "value"
this.reg = new Registry.Key(Registry.windef.HKEY.HKEY_CURRENT_USER, 'Path\Path\Path', Registry.windef.KEY_ACCESS.KEY_READ);
funct read(this.reg){
var value;
var pushes = [];
[
"food",
"veggy",
"etc",
"etc"].forEach(function(name) {
try {
value = key.getValue(name);
entries.push({name: name, value: value});
} catch (e) {
}
});
return pushes;
};
example: "food"="apple"
Which my code reads properly, however I came across a issue with special characters, example "ä"
"food"="äpple"
which my code reads as �pple.
My question is what kind of decoding/encoding should i use and what is with this win registry, what exactly are they using? Can it be raw JS preferably and if not what else? I tried using decodeURI/encodeURI but seems like its not the correct approach(dont know what encoding they are using and which decoding should I use)
TLDR: How can i type in "äpple" in win registry and when reading that file with JS get same "äpple" instead of "�pple"
It looks like you're using windows-registry-node. This is unfortunately a bug in that, #44. The reporter says:
If i return the raw buffer and use iconv to convert from "ISO-8859-1" to "UTF-8" i get the correct characters
Note that this is assuming the current code page of the system, though, and might not always be correct. (It might be possible to tell iconv to detect and use the current code page?)
The exact problem is in registry.js:
// READ VALUE
result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, value,
pKeyDataLength);
Here it uses RegQueryValueExA, which means fetch strings as the current Windows code page, as opposed to RegQueryValueExW which would use UTF-16. So value, which is a Node.JS Buffer, does not contain UTF-8. The code then calls Buffer.toString(), which assumes UTF-8 by default:
if (value.type === types.LPTSR) {
// TODO not sure why buffer's utf8 parsing leaves in the unicode null
// escape sequence. This is a work-around (at least on node 4.1)
value = value.toString().replace('\u0000', '');
}
So this is going to need a fix in windows-registry-node. The best fix is probably to set the code up for UTF-16, using the -W version of the function and value.toString('utf16le');
I'm trying to get the device locale by running the JavaScript in Tasker, the script should assign the locale to variable.
The whole JavaScript file looks like following:
var locale = Locale.getDefault();
But variable locale is empty.
I've also tried
var locale = Resources.getSystem().getConfiguration().getLocales().get(0);
with the same result.
Also tried to use String instead of var but that gave me an error.
Found a workaround.
In Tasker, you can run a Shell command, so I ran
getprop persist.sys.locale
and passed its result to a global value.
To use this value in my TypeScript I am getting it from my query string like this:
var UserName = #Request.QueryString["UserName"];
But I get a Unexpeted Identifier error on it because if in DevTool if I go to where it breaks that query string has a value like this:
var UserName = ANT -- ANT 37690 / THIRD PARTY
So is there a way to do some kind of sanitation on it so it wouldn't crash? I guess there are illegal characters in that value for JS?
The error has nothing to do with "special" characters, but with the fact that the right side of the assignment - unwrapped in quotes - contains what js engine views as unknown identifier[s].
One way to properly format data that becomes part of javascript code is to use JavaScriptSerializer class from System.Web.Script.Serialization namespace.
var UserName = #new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Request.QueryString["UserName"]);
The shorter version of this for a string is:
var UserName = "#System.Web.HttpUtility.JavaScriptStringEncode(Request.QueryString["UserName"])";
or overloaded version that wraps the result in double quotes:
var UserName = #System.Web.HttpUtility.JavaScriptStringEncode(Request.QueryString["UserName"], true);
You need to include quotes for the value.
var UserName = "#(Request.QueryString["UserName"])";
Otherwise the name will come through verbatim in your code and cause the problems you are seeing.
There is no need to protect against an attack vector here as the user can alter the page as they see fit at any time with a user script, and the QueryString is entered by them and only seen as a result by them in this scenario.
If there was a need to scrub the user input, it should be done prior to it actually reaching the view on server side. However, if still concerned about scrubbing output into a view in this type of scenario in general, it would be prudent to include an encode from razor's library.
var sanitizedJsVariable = "#System.Web.HttpUtility.JavaScriptStringEncode(model.VariableFromServer)";
Im new to Node.js and I'm trying to learn alone. I have a simple task to do the following:
-read a file (supplied buy the first command line argument).
-print the number of lines in the file.
-I am using the readFileSync method.
The code is running but the output is weird. I tried printing every statement, and I think the problem is in the reading of the data. Can anyone please tell me where I'm wrong?
function numLines(){
var fs = require('fs');
var num = 0;
var contents = fs.readFileSync(process.argv[0]);
console.log(contents.toString());
return num;
}
disregard the return statement, please just focus on the readFileSync. While printing the contents, the text printed is full of weird characters, as if the reading is going wrong
var contents = fs.readFileSync(process.argv[0]);
process.argv[0] is not the first argument passed to your script, actually. It's the command node. The second item is the filename of the your script, which is passed to node, assuming you're calling it as node myscript.js somefile.txt. You need to get the third item: process.argv[2]
See https://nodejs.org/api/process.html#process_process_argv
Without an encoding, readFile and readFileSync give you back a raw buffer you have to interpret in the correct encoding yourself (rather than just calling toString on it). From the docs:
If no encoding is specified, then the raw buffer is returned.
So either specify the encoding of the file in the readFileSync call, or use a buffer methods to read it using a given encoding. The Buffer docs talk about the encodings supported by Node, such as utf8.
So for instance, if your file is in UTF-8, you'd use:
function numLines(){
var fs = require('fs');
var num = 0;
var contents = fs.readFileSync(process.argv[0], {encoding: 'utf8'});
console.log(contents.toString());
return num;
}
More: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)