解决使用linux部署nodejs服务测试代码返回中文是乱码

发布时间:2022-07-04 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了解决使用linux部署nodejs服务测试代码返回中文是乱码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

今天写了个简单的node.js文件

  • 代码如下
VAR http = require('http');
http.createServer(function (request, response) {
    response.wrITeHead(200, { 'Content-tyPE': 'text/plain' });
    response.end('Hello World--测试n');
}).listen(8090);
console.LOG('Server running at http://127.0.0.1:8090/');

  • 部署成功以后,测试发现返回的中文是乱码,于是采用下面方式解决
  • 最终代码如下
var http = require('http');
http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/htML; charset=utf-8'});//只需要设置响应头的编码格式就好,解决中文乱码问题的代码
    // response.writeHead(200, { 'Content-Type': 'text/plain' }); // 原有代码
    response.end('Hello World--测试n');
}).listen(8090);
console.log('Server running at http://127.0.0.1:8090/');

代码修改一下,重启服务后,大功告成

脚本宝典总结

以上是脚本宝典为你收集整理的解决使用linux部署nodejs服务测试代码返回中文是乱码全部内容,希望文章能够帮你解决解决使用linux部署nodejs服务测试代码返回中文是乱码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。