Petar
Petar

Reputation: 339

UTF8 characters come out as windows-1252, ExpressJS running on Electron

A little bit of introduction - the (wannabe) software I'm working on is a desktop application built on Github Electron. It is designed to have two modules - a UI and a backend with an API, the second "living" in a hidden browser window and running on ExpressJS. The idea is for the UI and the API to communicate over HTTP so they could be decoupled if needed. All files are in UTF-8.

Now the problem itself - the main API route looks like:

router.get('/', (request, result) => {
  let message = 'Здрасти, коко!';

  console.log('Answering with ' + message);
  result.json(message);
});

When being called (from a browser, or Postman, or whatever), the answer looks like this:

{"message":"ЗдраÑти, коко!"}

...with those headers:

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 64
ETag: W/"40-3JawFDiTNEinvN6xFO6T9g"
Date: Tue, 20 Dec 2016 06:47:53 GMT
Connection: keep-alive

Using the 2cyr tool I found out that the source encoding is UTF-8 but it gets properly displayed only as windows-1252 which confuses me a lot.

To narrow down the possibilities, I added the console.log() to the route handler and (a bit surprising to me) I'm getting the same "broken" result in the Chromium debugger. I suspected the file's encoding, but this is what I get about it:

Petars-Mac:api petar$ file -I api.js
api.js: text/x-c++; charset=utf-8

The last thing that came to my mind was actually decoupling the API from Electron. When I run it in the terminal with node, I actually get the proper result - both in the log message in the terminal and in the JSON answer in the browser.

What am I doing wrong and what further debugging could I possibly do?

Upvotes: 2

Views: 1839

Answers (1)

Petar
Petar

Reputation: 339

So here we go, right before posting an issue in the Electron repository - it's the most stupid error I could imagine ever making in this situation.

TL;DR:

<meta charset="utf-8">

What I thought was that opening a second browser window for the backend and putting some JavaScript that runs in it would be enough. What I forgot was that it actually remains a browser window and therefore it needs just a little tiny bit of HTML to let it know that it serves UTF-8 content.

Maybe it's not me, maybe I was right expecting Express to serve UTF-8 over HTTP but nope. Anyway, it all works now.

Upvotes: 4

Related Questions