关于编码转换或许你从网络途径 得知是这样使用的
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);});