Reputation: 830
If you take the very basic, very simple example from the
node web page:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
which works great, but try to print something Cyrillic, like this:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Здравей Свят\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
You will get only badly decoded characters. I tried all kind of combinations with setting headers with different content-types, content-lengths, employing node-iconv module and other things, but i find my self going in circles.
Basically, I have mean-based web app and I just want to display Cyrillic text from my html/jade files to client browser. A few days digging already, with no results. I am sure if someone can tell what should be done in order for the above code to work, I will adapt it to my app (as I already am pretty sure, the issue is coming from some lower level, rather than from express midlewares etc..)
As far as I understood correctly - It seems to be an old issue originating somewhere in the way how javascript/v8 deal with utf-8. I saw plenty of other posts complaining about similar problems, but since I have tried to adapt each of them with no luck, I guess I will take the risk to post a duplicate question and have my chances...
some of the other posts/threads I visited:
node.js Nerve framework unicode response
Dealing with the Cyrillic encoding in Node.Js / Express App
http://dougal.gunters.org/blog/2012/03/14/dealing-with-utf-in-node-js/
Node.js public static folder to serve js with utf-8 charset
Why can't I write Chinese characters in nodejs HTTP response?
Any help will be greatly appreciated!
Upvotes: 5
Views: 6383
Reputation: 830
Alastair's comment solved my problem and fixed my headache :)
I am using WebStorm IDE and did not recall at all to check its own file encoding... Once i tried the node js example in notepad++, it worked directly. Even without the need for manually setting the charset.
It turned out that it is not needed to set manually the encoding via
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
So if anyone come to a similar problem I had - first verify the encoding of your source files!
Upvotes: 2