如何在 node.js 中将 HTTP 响应正文编码为 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19339069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:58:05  来源:igfitidea点击:

How to encode HTTP response body as UTF-8 in node.js

javascriptnode.jsutf-8

提问by Drake

This is currently my entire node.js server code:

这是目前我的整个 node.js 服务器代码:

require('http').createServer(function (req, resp) {
    var html = [
        '<!DOCTYPE html>',
        '<html>',
            '<head>',
                '<meta charset="utf-8" />',
                '<title>Sample Response</title>',
            '</head>',
            '<body>',
                '<p>Hello world</p>',
            '</body>',
        '</html>'
    ].join('');

    resp.writeHead(200, {
        'Content-Length': Buffer.byteLength(html, 'utf8'),
        'Content-Type': 'application/xhtml+xml;'
    });
    resp.write(html, 'utf8');
    resp.end();
}).listen(80);

Based on my understanding of the node.js documentation, the second 'utf8' argument to resp.write() should cause node to encode the html string as UTF-8, not the UTF-16 that JavaScript strings are natively represented as. However, when I point my browser to localhost:80, view the source, and save it to a local html file, Notepad++ tells me the file is encoded in UTF-16. Furthermore when I run it through the W3C html validator tool it also complains about "Internal encoding declaration utf-8 disagrees with the actual encoding of the document (utf-16)".

根据我对 node.js 文档的理解, resp.write() 的第二个 'utf8' 参数应该导致 node 将 html 字符串编码为 UTF-8,而不是 JavaScript 字符串本机表示为的 UTF-16。但是,当我将浏览器指向 localhost:80,查看源代码并将其保存到本地 html 文件时,Notepad++ 告诉我该文件以 UTF-16 编码。此外,当我通过 W3C html 验证器工具运行它时,它还会抱怨“内部编码声明 utf-8 与文档的实际编码 (utf-16) 不一致”。

How do I force node.js to encode my HTTP response body as UTF 8?

如何强制 node.js 将我的 HTTP 响应正文编码为 UTF 8?

回答by Jonathan Ong

maybe you have to do:

也许你必须这样做:

'Content-Type': 'application/xhtml+xml; charset=utf-8'

回答by AlbanMar31

According to : https://www.w3.org/International/articles/http-charset/indexand https://en.wikipedia.org/wiki/List_of_HTTP_header_fields.

根据:https://www.w3.org/International/articles/http-charset/indexhttps://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Recommend HTTP header looks like this:

推荐 HTTP 标头如下所示:

"Content-Type: text/html; charset=utf-8"

With the two codes below, it is possible to record in utf-8 with IE8 browser. Even if French XP32 does not allow the display of Thai characters in notepad ++.

有了下面的两个代码,就可以在 IE8 浏览器中以 utf-8 进行录制。即使法语 XP32 不允许在记事本 ++ 中显示泰语字符。

short form :

简写 :

var http = require('http');

var server = http.createServer(function(req, res) {
    var body = '<p>Hello D?m</p>\n \
  <p>How are you ?</p>\n \
  <p>????????(I am The Wolf)</p>';

  res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
  res.write(body, "utf-8");
  res.end(); 
});

server.listen(8080);

long form:

长表:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});

  var title = 'Sample Response'
  var body = '<p>Hello D?m</p>\n \
  <p>How are you ?</p>\n \
  <p>????????(I am The Wolf)</p>';

  var code =  [
        '<!DOCTYPE html>',
        '<html>',
            '<head>',
                '<meta charset="utf-8" />',
                '<title>' + title + '</title>',
            '</head>',
            '<body>',
                body,
            '</body>',
        '</html>'
    ].join('\n');

  res.write(code, "utf8");
  res.end(); 
});

server.listen(8080);

Thai characters are well preserved if I record from IE8 in an HTML page.

如果我从 IE8 记录在 HTML 页面中,泰语字符会得到很好的保留。

回答by Drake

Believe it or not, this problem I'm having with the internet is due to... Internet Explorer. In this case, Internet Explorer 11 for some reason thinks its acceptable to save the results of the View Source window in UTF-16 no matter what the original page encoding. So, my test page from localhost was saved as utf16, google.com was saved as utf16, etc. etc. Installed Firefox and its utf8 as far as the eye can see.

信不信由你,我在互联网上遇到的这个问题是由于... Internet Explorer。在这种情况下,Internet Explorer 11 出于某种原因认为无论原始页面编码是什么,都可以以 UTF-16 格式保存“查看源代码”窗口的结果。所以,我从本地主机的测试页面被保存为 utf16,google.com 被保存为 utf16,等等。安装了 Firefox 和它的 utf8。

I didn't believe them when they said IE is a terrible browser. I guess we all have to learn some time :(

当他们说 IE 是一个糟糕的浏览器时,我不相信他们。我想我们都必须学习一些时间:(