I am using node.js with express. I read out data from MongoDB with Mongoose and deliver it the normal way with res.send(data)
. Unfortunately the delivering fails for some requests. Even so the header says the encoding is utf-8, it seems to be ANSI in some cases, causing the jsonp callback function to fail with an error.
You can reproduce the error at this page: http://like-my-style.com/#!single/9837034 . The jsonp call fails just on some products, most of them (also the ones with special chars) work fine.
How can I ensure, that a given String is encoded in utf-8 in node.js?
Overview. 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.
In Node. js, the Buffer. toString() method is used to decode or convert a buffer to a string, according to the specified character encoding type. Converting a buffer to a string is known as encoding, and converting a string to a buffer is known as decoding.
This exception can be solved by increasing the default memory allocated to our program to the required memory by using the following command. Parameters: SPACE_REQD: Pass the increased memory space (in Megabytes).
Buffers have a toString() method that you can use to convert the buffer to a string. By default, toString() converts the buffer to a string using UTF8 encoding. For example, if you create a buffer from a string using Buffer. from() , the toString() function gives you the original string back.
Have you tried:
res.send(data.toString("utf8"));
To ensure that your data is in utf8 and is not Buffer.
I think I got stuck in a similar issue and neebz solution worked but I had to put it in the right spot.
var req = http.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
**res.setEncoding(encoding='utf8');**
res.on('data', function(d) {
console.log(d);
});
});
In the node.js docs its documented as request.setEncoding() which might be an error because it needs to be called on the res object that is created by the request.
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