zoukankan      html  css  js  c++  java
  • 解决使用linux部署nodejs服务测试代码返回中文是乱码

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

    • 代码如下
    var http = require('http');
    http.createServer(function (request, response) {
        response.writeHead(200, { 'Content-Type': 'text/plain' });
        response.end('Hello World--测试
    ');
    }).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--测试
    ');
    }).listen(8090);
    console.log('Server running at http://127.0.0.1:8090/');
    
    

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

  • 相关阅读:
    爬虫框架scrapy(1)持久化存储的多种方式及多页爬取数据
    爬虫之selenium
    redis相关
    爬虫之数据解析
    爬虫之requests模块2
    爬虫之requests模块
    HTTP和HTTPS协议
    Pymongo使用
    MongoDB
    python网络编程之黏包问题
  • 原文地址:https://www.cnblogs.com/sugartang/p/15334560.html
Copyright © 2011-2022 走看看