akr*_*smv 5 javascript utf-8 character-encoding node.js express
如果你举一个非常基本、非常简单的例子
\n\n节点网页:
\n\nvar http = require(\'http\');\n http.createServer(function (req, res) {\n res.writeHead(200, {\'Content-Type\': \'text/plain\'});\n res.end(\'Hello World\\n\');\n}).listen(1337, \'127.0.0.1\');\nconsole.log(\'Server running at http://127.0.0.1:1337/\');\nRun Code Online (Sandbox Code Playgroud)\n\n效果很好,但尝试打印一些西里尔字母,如下所示:
\n\nvar http = require(\'http\');\n http.createServer(function (req, res) {\n res.writeHead(200, {\'Content-Type\': \'text/plain\'});\n res.end(\'\xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd0\xb5\xd0\xb9 \xd0\xa1\xd0\xb2\xd1\x8f\xd1\x82\\n\');\n}).listen(1337, \'127.0.0.1\');\nconsole.log(\'Server running at http://127.0.0.1:1337/\');\nRun Code Online (Sandbox Code Playgroud)\n\n您只会得到解码错误的字符。我尝试了各种组合,设置不同内容类型、内容长度的标题,使用节点 iconv 模块和其他东西,但我发现自己在兜圈子。
\n\n基本上,我有基于平均值的 Web 应用程序,我只想将 html/jade 文件中的西里尔文本显示到客户端浏览器。挖了几天了,还没有结果。我确信如果有人可以告诉应该做什么才能使上述代码正常工作,我会将其调整到我的应用程序(因为我已经很确定,问题来自较低级别,而不是来自快速中间件等) ..)
\n\n据我正确理解 - 这似乎是一个老问题,起源于 javascript/v8 如何处理 utf-8 的方式。我看到很多其他帖子抱怨类似的问题,但由于我尝试适应每个问题但没有运气,我想我会冒险发布重复的问题并有机会......
\n\n我访问过的一些其他帖子/线程:
\n\n\n\n\n\n处理 Node.Js / Express App 中的西里尔字母编码
\n\nhttp://dougal.gunters.org/blog/2012/03/14/dealing-with-utf-in-node-js/
\n\nNode.js 公共静态文件夹,用于使用 utf-8 字符集提供 js
\n\n\n\n任何帮助将不胜感激!
\n阿拉斯泰尔的评论解决了我的问题并解决了我的头痛:)
我正在使用 WebStorm IDE,根本不记得检查它自己的文件编码...一旦我在 notepad++ 中尝试了 Node js 示例,它就直接工作了。即使不需要手动设置字符集。
事实证明,不需要通过手动设置编码
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
Run Code Online (Sandbox Code Playgroud)
因此,如果有人遇到我遇到的类似问题 - 首先验证源文件的编码!
| 归档时间: |
|
| 查看次数: |
5790 次 |
| 最近记录: |