一、控制台输出乱码
1.Preferences-> Browser Packages
2.目录下找到Nodejs.sublime-build
3.改其中的encoding为utf-8即可, 默认为cp1252
{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell":true,
"encoding": "utf-8",// cp1252 -> utf-8
"windows":
{
"cmd": ["taskkill /F /IM node.exe & node", "$file"]
},
"linux":
{
"cmd": ["killall node; node", "$file"]
}
}
二、要想Node.js正常显示中文,需要:
1、js文件保存为unicode格式。js文件是否为unicode格式,一个简单的方法是使用记事本来判断。使用记事本打开JS文件,点菜单另存为,看编码格式是否为”UTF-8”。若不是,可使用UltraEdit工具进行转换,使用记事本也可以转换。
2、在js文件中增加编码说明meta数据,让浏览器知道使用什么编码来解释网页
一个Node.js中使用中文的例子如下,该app.js需保存为utf-8格式,同时在文中增加meta编码数据说明:
<meta charset="utf-8"/>
[javascript] view plain copy
//app.js
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<head><meta charset="utf-8"/></head>');
res.write('<h1>Node.js</h1>');
res.write('<b>亲爱的,你慢慢飞,小心前面带刺的玫瑰...</b>');
res.end('<p>Hello World</p>');
}).listen(3000);
console.log("HTTP server is listening at port 3000.");
三、装一下解决乱码的插件