This is currently all the node.js server code:
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 argument to 'utf8' for resp.write () should force node to encode the html string as UTF-8, and not UTF-16, which JavaScript strings are originally represented as. However, when I point my browser to localhost: 80, look at the source code and save it in a local html file, Notepad ++ tells me that the file is encoded in UTF-16. In addition, when I run it through the W3C HTML code checker, he also complains about "The declaration of the internal encoding utf-8 is not consistent with the actual encoding of the document (utf-16)."
How to force node.js to encode the HTTP response body as UTF 8?