开发者

node.js Nerve framework unicode response

code:

var nerve = require("./nerve");
var sitemap = [
    ["/", function(req, res) {
        res.respond("Русский");
    }]
];
nerve.create(sit开发者_JS百科emap).listen(8100);

show in browser:

CAA:89  

How it should be correct?


Nerve appears to interpret the strings you pass as binary strings, which results in the output you’re seeing. You can use the Buffer class to convert your UTF-8 chars to a binary string manually. You also need to set the charset in your headers:

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

If you want to try another framework, Express does a better job handling UTF-8. It interprets strings as UTF-8 and sets the charset correctly by default:

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

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

app.listen(8100);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜