unicode - node.js Nerve 框架 unicode 响应

标签 unicode utf-8 node.js

代码:

var nerve = require("./nerve");
var sitemap = [
    ["/", function(req, res) {
        res.respond("Русский");
    }]
];
nerve.create(sitemap).listen(8100);

在浏览器中显示:

CAA:89  

它应该如何正确?

最佳答案

神经appears将您传递的字符串解释为二进制字符串,这会导致您看到的输出。您可以使用 Buffer 类手动将 UTF-8 字符转换为二进制字符串。您还需要在 header 中设置字符集:

var sitemap = [
  ["/", function (req, res) {
    res.respond({
      headers: {"Content-Type": "text/html; charset=utf-8"},
      content: new Buffer("Русский", "utf8").toString("binary")
    });
  }]
];

如果你想尝试其他框架,Express更好地处理 UTF-8。它将字符串解释为 UTF-8 并默认正确设置字符集:

var app = require("express").createServer();

app.get("/", function (req, res) {
  res.send("Русский");
});

app.listen(8100);

关于unicode - node.js Nerve 框架 unicode 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807576/

相关文章:

Python 将 latin1 转换为 UTF8

utf-8 - 下标字母的UTF8符号

php - file_get_contents() 将 UTF-8 转换为 ISO-8859-1

javascript - 使用 JavaScript 调用 Node.js 后端函数的最有效方法是什么

c - 错误 : wide character array initialized from incompatible wide string

java - 'the local character set' 与 'the encoding of the text data you want to process' 相同吗?

php - 从android发送数据到php,php存储一个序列?在mysql中

python - 如何从 URL 参数中获取 unicode 字符?

javascript - RTS HTML5 多人游戏 - 启动 node.js 服务器 ingame

javascript - 陷入实现嵌套数据过滤循环的困境,这可能是一种更优雅的方法吗?