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

How to solve the problem that Docker container has no vim command

Find the problem Today, when I tried to modify th...

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

Vue.js handles Icon icons through components

Icon icon processing solution The goal of this re...

How to pop up a temporary QQ dialog box to chat online without adding friends

In fact, this is very simple. We add an a tag to ...

Css3 realizes seamless scrolling and anti-shake

question The seamless scrolling of pictures and t...

How to replace all tags in html text

(?i) means do not match case. Replace all uppercas...

Detailed introduction to MySQL database index

Table of contents Mind Map Simple understanding E...

HTML5+CSS3 coding standards

The Golden Rule No matter how many people are wor...

How to install docker on ubuntu20.04 LTS

Zero: Uninstall old version Older versions of Doc...

Detailed explanation of the function and usage of DOCTYPE declaration

1. Browser rendering mode and doctype Some web pa...

Centos7.5 installs mysql5.7.24 binary package deployment

1. Environmental preparation: Operating system: C...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

A nice html printing code supports page turning

ylbtech_html_print HTML print code, support page t...