node.js Nerve framework unicode response

2019-08-14 23:53发布

code:

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

show in browser:

CAA:89  

How it should be correct?

1条回答
SAY GOODBYE
2楼-- · 2019-08-15 00:38

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);
查看更多
登录 后发表回答