browser-sync: Charset problem

I’m having problems with charset, naturally the codes in .aspx need (and I don’t know ecxacly why) the ISO-8859-1. Prints someting like this: *capacita��o de *

        browserSync: {
            geral: {
                bsFiles: {
                    src: [
                        '**/*.html',
                        '**/*.css',
                        '**/*.min.js',
                        '**/*.aspx',
                        '**/*.master',
                        '**/*.ashx'
                    ]
                },
                options: {
                    watchTask: true,
                    startPath: "/default.aspx",
                    proxy: config.host + ':' + config.port,
                    notify: false,
                    open: false
                }
            }
        },

With aspx, ours programmers insert the content of news, in web.config I found this. http://prntscr.com/7eitat

But if i change the value of charset here, nothing more works.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 5
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I’m trying to use the code above but I’m getting the following error:

node:_http_server:279
    throw new ERR_HTTP_INVALID_STATUS_CODE(originalStatusCode);
    ^

RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined

Any idea what could be missing in my case?

Fixed version. Tested node v17.9.0.

var browserSync = require("browser-sync").create();
var iconv = require("iconv-lite");

browserSync.init({
  watch: false,
  proxy: {
    target: "http://mydomain.local",
    proxyRes: [
      (proxyRes, req, res) => {
        if (
          proxyRes.headers &&
          proxyRes.headers["content-type"] &&
          proxyRes.headers["content-type"].match("text/html")
        ) {
          const _end = res.end;
          const _writeHead = res.writeHead;
          let buffer = Buffer.from("");

          proxyRes.on("data", chunk => {
            buffer = Buffer.concat([buffer, chunk]);
          });

          res.write = () => {};
          res.writeHead = (...args) => {
            _writeHead.apply(res, args);
          };

          res.end = () => {
            _end.call(res, iconv.decode(buffer, "iso-8859-1"));
          };
        }
      },
      function(res) {
        if (
          res.headers &&
          res.headers["content-type"] &&
          res.headers["content-type"].match("text/html")
        )
        res.headers["content-type"] = "text/html; charset=utf-8";
      }
    ]
  }
});

Based on @kokarn’s and @youngzhaosignifyd’s examples and with the help of iconv-lite, plus using proxyRes option I am successfully using this script:

proxyRes: [
  (proxyRes, req, res) => {
    if( proxyRes.headers && proxyRes.headers["content-type"] &&
        proxyRes.headers["content-type"].match("text/html") ) {
            
      const _end = res.end
      const _writeHead = res.writeHead
      let writeHeadArgs
      let buffer = new Buffer("")

      proxyRes.on("data", (chunk) => {
        buffer = Buffer.concat([buffer, chunk])
      })

      res.write = () => {};
      res.writeHead = (...args) => {writeHeadArgs = args}

      res.end = () => {
        _writeHead.apply(res, writeHeadArgs)
        _end.call(res, iconv.decode(buffer, "iso-8859-2"))
      }
    }
  }
]