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");
该文章同步在:
简书 : https://www.jianshu.com/users/63adfe13324c/timeline
GitHub Page : https://levinhax.github.io/