關於編碼轉換或許你從網絡途徑得知是這樣使用的
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")) });
上面輸出的結果還是亂碼,並不能解碼gbk 先最新的iconv不支持string解碼了
查看最新官方文檔下面是正確的解碼方式
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); });