【发布时间】:2015-06-19 02:09:03
【问题描述】:
如果你从
node网页:
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/');
效果很好,但尝试打印一些西里尔文,如下所示:
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/');
您只会得到解码错误的字符。我尝试了各种组合,设置不同内容类型、内容长度的标题,使用 node-iconv 模块和其他东西,但我发现我自己在绕圈子。
基本上,我有一个基于平均值的网络应用程序,我只是想将我的 html/jade 文件中的西里尔文文本显示到客户端浏览器。已经挖了几天,没有结果。我敢肯定,如果有人能告诉我应该做什么才能使上述代码正常工作,我会将其调整到我的应用程序中(因为我已经很确定,问题来自较低级别,而不是来自快速中间件等..)
据我正确理解 - 这似乎是一个老问题,起源于 javascript/v8 如何处理 utf-8 的方式。我看到很多其他帖子都在抱怨类似的问题,但是由于我尝试适应每个帖子都没有运气,我想我会冒险发布一个重复的问题并有机会......
我访问过的其他一些帖子/主题:
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?
任何帮助将不胜感激!
【问题讨论】:
-
尝试显式设置编码
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'}); -
另外,确保您的源文件采用 UTF-8 编码:stackoverflow.com/questions/10125141/…
-
Alastair 的评论解决了我的问题并解决了我的头痛 :) 我正在使用 WebStorm IDE 并且根本不记得检查它自己的文件编码......一旦我在 notepad++ 中尝试了节点 js 示例,它直接工作。即使不需要手动设置字符集。谢谢阿拉斯泰尔!
标签: javascript node.js express utf-8 character-encoding