Node.js solves the problem of Chinese garbled characters in client request data

Node.js solves the problem of Chinese garbled characters in client request data

Node.js solves the problem of Chinese garbled characters in client request data

Example code:

var http = require('http');
var server = http.createServer();
server.on('request',function(req,res){
    // res.end("hello world");
    res.end("Hello World");
});
server.listen(3000,function(){
    console.log("Server is running");
});

insert image description here

reason:

The data sent by default on the server is actually UFT8 encoded content

But the browser does not know that you are UFT8 encoded content

If the browser does not know the encoding of the server's response content, it will execute it according to the default encoding of the current operating system.

The default setting for Chinese operating systems is GBK

Solution: The correct way is to tell the browser what type of data I am sending you res.setHeader('Content-Type','text/plain;charset=utf-8'); , be careful not to make a mistake in the connector, and don't write utf-8 randomly. In the http protocol, content-Type is used to tell the other party what type of data I am sending to you, and then the type is written immediately afterwards.

var http = require('http');
var server = http.createServer();
server.on('request',function(req,res){
    // res.end("hello world");
    res.setHeader('Content-Type','text/plain;charset=utf-8');
    res.end("Hello World");
});
server.listen(3000,function(){
    console.log("Server is running");
});

insert image description here
Note that there are many types

Response content type Content-Type

var http = require('http');
var server = http.createServer();
server.on('request',function(req,res){
    if(req.url==='/plain'){
        res.setHeader('Content-Type','text/plain;charset=utf-8');
        res.end("Hello World");
    }else if(req.url==='/html'){
        res.setHeader('Content-Type','text/html;charset=utf-8');
        res.end("<h1>Hello World<br/> hello world</h1>");
    }
   
});
server.listen(3000,function(){
    console.log("Server is running");
});

Return different types of Content-Type formats according to different request paths

insert image description here

This is the end of this article about node.js's method to solve the problem of Chinese garbled characters in client request data. For more relevant node client request data garbled characters content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A simple udp broadcast server and client implemented by Nodejs
  • Detailed explanation of node HTTP request client-Request
  • http request client example in Node.js (request client)
  • Example of server and client functions implemented by nodejs socket
  • NodeJS implements client-side js encryption
  • Detailed explanation of Nodejs TCP server and client chat program
  • NodeJS http module usage example [Creating a web server/client]
  • Node.js uses the http module to create a complete example of server and client

<<:  A detailed introduction to Linux system configuration (service control)

>>:  Two ways to implement HTML to randomly drag content positions

Recommend

The presentation and opening method of hyperlink a

<br />Related articles: How to prompt and op...

CSS sets the list style and creates the navigation menu implementation code

1. Set the list symbol list-style-type: attribute...

Detailed steps to store emoji expressions in MySQL

Caused by: java.sql.SQLException: Incorrect strin...

Javascript tree menu (11 items)

1. dhtmlxTree dHTMLxTree is a feature-rich Tree M...

SQL Aggregation, Grouping, and Sorting

Table of contents 1. Aggregate Query 1. COUNT fun...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

How to view image information in Docker

In this article, we will need to learn how to vie...

How to allow external network access to mysql and modify mysql account password

The root account of mysql, I usually use localhos...

Summary of special processing statements of MySQL SQL statements (must read)

1. Update the entire table. If the value of a col...

CentOS method to modify the default ssh port number example

The default ssh port number of Linux servers is g...

XHTML Tutorial: The Difference Between Transitional and Strict

In fact, XHTML 1.0 is divided into two types (thr...

MySQL 5.7.21 installation and configuration tutorial

The simple installation configuration of mysql5.7...