How to fix charset="UTF-8" in application nodejs within the socket.io

Asked

Viewed 2,482 times

0

I have a chat app and I’m working with nodejs and socket.io. But I’m having trouble with name coding. The following name: Guilherme Loução is coming as: Guilherme Louã§. I have already saved the file with UTF-8 format and it has not solved. There is some module to fix this inside the socket.io?

Code:

...
io.sockets.on('connection', ioJwt.authorize({
  secret: jwtSecret,
  timeout: 15000
})).on('authenticated', function(socket) {

  socket.on('join', function(data){

    console.log(data.name);
  });
  ...
});
...

1 answer

1


Your page must be using iso-8859-1/windows-1252 and the JSON response returns in utf-8, you can change the page (or add-on) to use the:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

It is also necessary save the documents (if you have any, such as .html or templates) like UTF-8 without GOOD.

However if you want to use iso-8859-1 you can try to decode from utf-8, first install this via npm:

npm install utf8

Then call it that:

var utf8 = require('utf8');

Then use:

console.log( utf8.decode(data.name) );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.