Home >Web Front-end >JS Tutorial >Invalid encoding of nodejs iconv Description
About encoding conversion, maybe you learned from the Internet that it is used in this way
var fs = require("fs"); var iconv=require("iconv-lite"); var request=require("request"); var txt=fs.readFileSync("./a.txt","utf8"); iconv.decode(txt,"gbk") request("http://jd.com",function(err,res,body){ console.log(iconv.decode(body,"gbk")) });
The above output result is still garbled and cannot decode gbk. First, the latest iconv does not support string decoding.
Check the latest official documents. The following is the correct decoding method.
var fs = require("fs"); var iconv=require("iconv-lite"); var request=require("request"); var txt=fs.readFileSync("./a.txt"); iconv.decode(txt,"gbk") request("http://jd.com") .pipe(iconv.decodeStream('gbk')) .collect(function(err, decodedBody) { fs.writeFile("./jd.txt",decodedBody); });