美文网首页代码改变世界
Node.js中文乱码解决

Node.js中文乱码解决

作者: BigDipper | 来源:发表于2015-01-30 10:45 被阅读6995次

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

所以,要确保不出现乱码,应做到以下两点:

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

  • 在你的JS文件中的 writeHead 方法中加入 "charset=utf-8" 编码,如下例所示:
    var http = require("http");

      http.createServer(function (req, res) {
          res.writeHead(200, {
              "Content-Type": "text/html;charset=utf-8"
          });
    
          res.end("<h1>你好</h1>");
      }).listen(3000);

相关文章

网友评论

    本文标题:Node.js中文乱码解决

    本文链接:https://www.haomeiwen.com/subject/lvelxttx.html