30 Days of node
Day 22 : String Decoder Module in node.js

30 days of node - Nodejs tutorial series
String Decoder

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 methods

String decoder class has only 2 methods which are as follows :

  1. stringDecoder.write(buffer) : This method is used to return the specified buffer as decoded string. We pass 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
    //Print the decoded buffer  

    We can run it in the following way :
    >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

  2. stringDecoder.end([buffer]) : This method is used to return the remaining of the input stored in internal buffer.


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