处理西里尔编码的节点js有什么问题
What is wrong with node js dealing with cyrillic encoding
如果您使用
中非常基本、非常简单的示例
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/');
您只会得到解码错误的字符。我尝试了各种组合,设置 headers 与不同的 content-types、content-lengths、使用 node-iconv 模块和其他东西,但我发现自己在兜圈子。
基本上,我有 mean-based 网络应用程序,我 只是 想将我的 html/jade 文件中的西里尔文文本显示到客户端浏览器。已经挖了几天,没有结果。我敢肯定,如果有人可以告诉我应该做什么才能使上述代码正常工作,我会将其调整为我的应用程序(因为我已经很确定,问题来自某个较低级别,而不是来自 express 中间件等..)
据我正确理解 - 这似乎是一个老问题,起源于 javascript/v8 如何处理 utf-8。我看到很多其他 post 抱怨类似的问题,但由于我尝试调整每一个都没有运气,我想我会冒险 post 一个重复的问题并有机会...
其他一些 posts/threads 我访问过:
Encoding error with node.js
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
任何帮助将不胜感激!
Alastair 的评论解决了我的问题并解决了我的头痛问题:)
我正在使用 WebStorm IDE 并且根本不记得检查它自己的文件编码...一旦我在 notepad++ 中尝试了节点 js 示例,它就直接工作了。即使不需要手动设置字符集。
原来不需要通过
手动设置编码
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
所以如果有人遇到我遇到的类似问题 - 首先验证源文件的编码!
如果您使用
中非常基本、非常简单的示例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/');
您只会得到解码错误的字符。我尝试了各种组合,设置 headers 与不同的 content-types、content-lengths、使用 node-iconv 模块和其他东西,但我发现自己在兜圈子。
基本上,我有 mean-based 网络应用程序,我 只是 想将我的 html/jade 文件中的西里尔文文本显示到客户端浏览器。已经挖了几天,没有结果。我敢肯定,如果有人可以告诉我应该做什么才能使上述代码正常工作,我会将其调整为我的应用程序(因为我已经很确定,问题来自某个较低级别,而不是来自 express 中间件等..)
据我正确理解 - 这似乎是一个老问题,起源于 javascript/v8 如何处理 utf-8。我看到很多其他 post 抱怨类似的问题,但由于我尝试调整每一个都没有运气,我想我会冒险 post 一个重复的问题并有机会...
其他一些 posts/threads 我访问过:
Encoding error with node.js
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
任何帮助将不胜感激!
Alastair 的评论解决了我的问题并解决了我的头痛问题:)
我正在使用 WebStorm IDE 并且根本不记得检查它自己的文件编码...一旦我在 notepad++ 中尝试了节点 js 示例,它就直接工作了。即使不需要手动设置字符集。
原来不需要通过
手动设置编码 res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
所以如果有人遇到我遇到的类似问题 - 首先验证源文件的编码!