Saturday, 10 June 2017

Browser-sync change responce encoding to iso-8859-1

I'm proxying my site through browser-sync, the site uses iso-8859-1 encoding. NodeJS does not support iso-8859-1 encoding so I'm trying to change the response encoding with iconv-lite.

This is the code so far:

browserSync.init({
    proxy: {
        target: 'https://www.example.com',
        proxyReq: [
            function (proxyReq) {
                proxyReq.on('response', function (response) {
                    if (response.headers['content-type'].indexOf('text/html') !== -1) {
                        var bufs = [], bufsize = 0;
                        response.on('data', function (data) {
                            bufs.push(data);
                            bufsize += data.length;
                        }).on('end', function () {
                            var buffer = Buffer.concat(bufs, bufsize),
                                body = iconv.decode(buffer, 'iso-8859-1');
                            console.log('Body: ' + body);
                        });
                    }
                })
            }
        ]
    }
});

The content is correctly converted to iso-8859-1, but this code does not change the response it self.

How do I modify the response before it is returned?



via projectIncomplete

No comments:

Post a Comment