NodeJS 中文乱码解决

如果使用NodeJS 。当有中文时,如果不做任何处理就会出现乱码。因为,NodeJS 不支持 GBK。当然,UTF-8是支持的。所以,要确保不出现乱码:

 1.保证你的 JS文件是以UTF-8格式保存的。

 2. 在你的JS文件中的 writeHead 方法中加入 "charset=utf-8" 编码,如下例所示:

  var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'});
  
 // response.write("程明卫");
  response.end('程明卫,Hello World\n');
}).listen(8124);

console.log('Server running at http://127.0.0.1:8124/');

评论

暂无评论