简体   繁体   English

为什么我不能在nodejs HTTP响应中写中文字符?

[英]Why can't I write Chinese characters in nodejs HTTP response?

Here is my little code: 这是我的小代码:

var http = require('http');
var port = 9002;
var host_ip = '<my_ip>';
http.createServer(function (req, res) {
    var content = new Buffer("Hello 世界", "utf-8")
    console.log('request arrived');
    res.writeHead(200, {
        'Content-Encoding':'utf-8',
        'charset' : 'utf-8',
        'Content-Length': content.length,
        'Content-Type': 'text/plain'});
    res.end(content.toString('utf-8'),'utf-8');
}).listen(port, host_ip);
console.log('server running at http://' + host_ip + ':' + port);

Previously I just let res.end to send "hello world" and it worked well. 以前我只是让res.end发送“你好世界”,它运作良好。 Then I wanted to adjust a little bit and changed the 'world' into the Chinese equivalent '世界', and so changed the 'charset' 'content-type' in the header to 'utf-8'. 然后我想调整一点,将'世界'改成中文等同的'世界',然后将标题中的'charset''内容类型'改为'utf-8'。 But in Chrome and Firefox I see this: 但在Chrome和Firefox中我看到了这个:

hello 涓栫晫

However, amazingly opera(11.61) does show the correct result hello 世界 . 然而,令人惊讶的是歌剧(11.61)确实显示了正确的结果hello 世界 I want to know whether I have missed something in the code, and why this is happening. 我想知道我是否错过了代码中的某些内容,以及为什么会发生这种情况。 Thank you guys. 感谢你们。

I think this post is similiar with my situation but not exactly. 我认为这篇文章与我的情况类似,但并不完全相同。

Problem is with the character set specification. 问题在于字符集规范。 For me it works with this change: 对我来说,这适用于这种变化:

'Content-Type': 'text/plain;charset=utf-8'

Tested with Chrome, Firefox and Safari. 使用Chrome,Firefox和Safari进行测试。

You could also look into the node.js package "express" which allows rewriting your code like this: 您还可以查看node.js包“express”,它允许重写您的代码,如下所示:

var express=require('express');

var app=express.createServer();

app.get('/',function(req, res) {
    var content = "Hello 世界";

    res.charset = 'utf-8';
    res.contentType('text');
    res.send(content);
});

app.listen(9002);

content-encoding is not a character set but a encoding of http response itself content-encoding不是字符集,而是http响应本身的编码

charset is not a common http header charset不是常见的http标头

content-length is unneccesary here 这里不需要content-length

as @jjrv said, you should write 'Content-Type': 'text/plain;charset=utf-8' there 正如@jjrv所说,你应该写'Content-Type': 'text/plain;charset=utf-8'那里

涓栫晫 is actually 世界 in encoding GB-18030, and then displayed as UTF-8. 涓栫晫实际上是编码GB-18030的世界 ,然后显示为UTF-8。 Probably the characters got saved in that encoding. 可能角色在该编码中得到了保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM