node.js神经框架unicode响应 [英] node.js Nerve framework unicode response

查看:93
本文介绍了node.js神经框架unicode响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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

在浏览器中显示:

CAA:89  

应该如何正确?

推荐答案

神经

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")
    });
  }]
];

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

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);

这篇关于node.js神经框架unicode响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆