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

2021/9/26 7:10:58

本文主要是介绍解决使用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服务测试代码返回中文是乱码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程