Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert data to utf-8 in node.js?

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?

like image 922
Thomas Avatar asked Apr 18 '11 21:04

Thomas


People also ask

What is UTF8 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.

How do I decode buffer data in node JS?

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.

How do I fix a process out of memory exception in node JS?

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).

How do I convert a buffer to a string?

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.


2 Answers

Have you tried:

res.send(data.toString("utf8"));

To ensure that your data is in utf8 and is not Buffer.

like image 180
Van Nguyen Avatar answered Sep 23 '22 05:09

Van Nguyen


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.

like image 42
bdavis Avatar answered Sep 20 '22 05:09

bdavis