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

可紊 提交于 2019-12-01 17:07:25

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.

You could also look into the node.js package "express" which allows rewriting your code like this:

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

charset is not a common http header

content-length is unneccesary here

as @jjrv said, you should write 'Content-Type': 'text/plain;charset=utf-8' there

涓栫晫 is actually 世界 in encoding GB-18030, and then displayed as UTF-8. Probably the characters got saved in that encoding.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!