


About Nodejs server-side character encoding, decoding and garbled processing
This article mainly introduces the advanced server-side character encoding and decoding and garbled processing of Nodejs. It has certain reference value. Interested friends can refer to it
Written in front
In web server development, character encoding and decoding have to be dealt with almost every day. Once the encoding and decoding is not handled properly, troublesome garbled characters will occur.
Many students who are engaged in node server development often find themselves at a loss when encountering problems due to insufficient knowledge of character encoding codes and spend a lot of time troubleshooting and solving problems.
The text first briefly introduces the basic knowledge of character encoding and decoding, then gives an example of how to encode and decode in node, and finally is a server-side code example. Code examples related to this article can be found here.
About character encoding and decoding
In the process of network communication, binary bits are transmitted, regardless of whether the content sent is text or pictures, the language used Is it Chinese or English.
For example, the client sends "Hello" to the server.
Client --- Hello ---> Server
This contains two key steps, corresponding to encoding and decoding.
1. Client: Encode the string "Hello" into the binary bits required by the computer network.
2. Server: Decode the received binary bits into the string "Hello".
To summarize:
1. Encoding: Convert the data to be transmitted into the corresponding binary bits.
2. Decoding: Convert binary bits into original data.
Some important technical details are not mentioned above, the answer is in the next section.
How does the client know the number of bits corresponding to the character "Hello"?
After the server receives the binary bits, how does it know what the corresponding string is?
About character set and character encoding
The problem of character and binary conversion is mentioned above. Since the two can be converted to each other, that is to say, there are clear conversion rules, and the characters can be converted into binary characters.
The conversion rules mentioned here are actually the character sets & character encodings we often hear.
Character set is a collection of a series of characters (text, punctuation marks, etc.). There are many character sets, common ones include ASCII, Unicode, GBK, etc. The main difference between different character sets is the number of characters they contain.
After understanding the concept of character set, let’s introduce character encoding.
The character set tells us which characters are supported, but how to encode specific characters is determined by the character encoding. For example, the Unicode character set supports character encodings such as UTF8 (commonly used), UTF16, and UTF32.
To summarize:
Character set: A collection of characters. Different character sets contain different numbers of characters.
Character encoding: The actual encoding of characters in the character set.
A character set may have multiple character encoding methods.
Character encoding can be regarded as a mapping table. The client and server use this mapping table to implement character and binary encoding and decoding conversion.
For example, the character "you" occupies three bytes 0xe4 0xbd 0xa0 in UTF8 encoding, and occupies two bytes 0xc4 0xe3 in GBK encoding.
Character encoding and decoding examples
The basic knowledge required for character encoding and decoding has been mentioned above. Let's look at a simple example below, where we use the icon-lite library to help us implement encoding and decoding operations.
As you can see, we use gbk when encoding characters. When decoding, if you also use gbk, you can get the original characters. When we use utf8 when decoding, garbled characters appear.
var iconv = require('iconv-lite'); var oriText = '你'; var encodedBuff = iconv.encode(oriText, 'gbk'); console.log(encodedBuff); // <Buffer c4 e3> var decodedText = iconv.decode(encodedBuff, 'gbk'); console.log(decodedText); // 你 var wrongText = iconv.decode(encodedBuff, 'utf8'); console.log(wrongText); // ��
Practical example: server-side encoding and decoding
Usually we need to deal with encoding and decoding scenarios involving file reading and writing , Network request processing. Here is an example of a network request, introducing how to encode and decode on the server side.
Suppose we are running the following http service, listening for requests from clients. The client uses gbk
encoding when transmitting data, while the server uses utf8
encoding by default.
If the default utf8
is used to decode the request at this time, garbled characters will appear, so special processing is required.
The server code is as follows (to simplify the code, the judgment of the request method and request encoding is skipped here)
var http = require('http'); var iconv = require('iconv-lite'); // 假设客户端采用post方法,编码为gbk var server = http.createServer(function (req, res) { var chunks = []; req.on('data', function (chunk) { chunks.push(chunk) }); req.on('end', function () { chunks = Buffer.concat(chunks); // 对二进制进行解码 var body = iconv.decode(chunks, 'gbk'); console.log(body); res.end('HELLO FROM SERVER'); }); }); server.listen(3000);
The corresponding client The code is as follows:
var http = require('http'); var iconv = require('iconv-lite'); var charset = 'gbk'; // 对字符"你"进行编码 var reqBuff = iconv.encode('你', charset); var options = { hostname: '127.0.0.1', port: '3000', path: '/', method: 'POST', headers: { 'Content-Type': 'text/plain', 'Content-Encoding': 'identity', 'Charset': charset // 设置请求字符集编码 } }; var client = http.request(options, function(res) { res.pipe(process.stdout); }); client.end(reqBuff);
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About the simple communication function between nodejs socket server and client
How to use ES6 in NodeJS projects
The above is the detailed content of About Nodejs server-side character encoding, decoding and garbled processing. For more information, please follow other related articles on the PHP Chinese website!

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
