String decoder module in node.js is used to provide an API for decoding buffer object into strings. Also , this decoding is performed in a manner that
UTF-8 and UTF-16 characters multibyte coding is preserved. We can require the query string module in the following way :
var sd = require('string_decoder').StringDecoder;
String decoder class has only 2 methods which are as follows :
buffer
as the argument in this method . An example is given below :
//name of the file : write.js
var stringDecoder = require('string_decoder').StringDecoder;
var sd = new stringDecoder('utf8');
var buff = Buffer('data to be buffered');
//Print the buffered data
console.log(buff);
//Print the decoded buffer
console.log(sd.write(buff));
>node write.js
<Buffer 64 61 74 61 20 74 6f 20 62 65 20 62 75 66 66 65 72 65 64>
data to be buffered
In this chapter of 30 days of node tutorial series, we learned about String decoder method in node.js which is used
for decoding buffer object into strings. We also learned about two methods of string decoder which are stringDecoder.write(buffer)
and
stringDecoder.end([buffer])
.