node.js read non utf-8 http response
Hello from past, if you gonna need to read something like windows-1251
from the web following snippet may be helpful
import https from 'https'
import fs from 'fs'
import {Iconv} from 'iconv'
https.get('https://some-old-website.com/foo/bar', res => {
res.setEncoding('binary')
let chunks = []
res.on('data', chunk => chunks.push(Buffer.from(chunk, 'binary')))
res.on('end', () => {
const html = Iconv('windows-1251', 'utf8').convert(Buffer.concat(chunks)).toString()
fs.writeFileSync('list.html', html, 'utf-8')
})
})