Node.js對中文支持不太好,有時侯打開文件會出現亂碼。要想正常顯示中文,需要注意以下兩點:
保證.js文件保存為unicode格式,編碼格式為"UTF-8"
在你的JS文件中的http.ServerResponse對象中的writeHead方法中加入 "charset=utf-8" 語句,定義文件中所使用的字符編碼
下面是一個簡單的示例:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {
'content-type': 'text/plain;charset=utf8'
});
response.write("這里顯示一段中文");
response.end("");
}).listen(8124);
console.log("Server running on 8124");
該文章同步在:
CSDN Blog : http://blog.csdn.net/levinhax/article/details/77170498
GitHub Page : https://levinhax.github.io/