Home >Web Front-end >Front-end Q&A >Garbled Chinese characters received by nodejs

Garbled Chinese characters received by nodejs

PHPz
PHPzOriginal
2023-05-08 09:33:06862browse

With the development of the Internet, many developers have begun to use Node.js for back-end development. However, some developers will encounter the problem of garbled Chinese characters when using Node.js to receive Chinese characters. This question is both confusing and difficult. This article will introduce the problem of garbled Chinese characters received by Node.js and provide several solutions.

Problem description

When using Node.js to receive requests containing Chinese characters, sometimes garbled characters will appear, as shown in the following figure:

As shown in the figure above, the received request parameters contain Chinese characters, but they are converted into garbled characters. This is obviously not the result we want, so where is the problem?

Problem Analysis

First of all, we need to understand the principle of Node.js receiving request parameters.

Node.js is a high-performance platform based on event-driven and non-blocking I/O models. It listens to the network port. When a request is received, it triggers an event and encapsulates the request into a request object. The request object contains request headers, request body and other information. When receiving the request body, Node.js uses UTF-8 encoding for decoding by default.

So, if the request body we receive contains Chinese characters, and the Content-Type in the request header does not set the character set encoding, UTF-8 will be used for decoding by default. If characters are set in the request header Set encoding, a specific character set will be used for decoding. However, if the character set encoding is not set in the request header and the request body contains multiple character set encodings or garbled characters, Node.js may cause garbled characters when decoding.

Solution

For the problem of garbled Chinese characters received by Node.js, we can start from the following aspects:

  1. Uniform Character Set Encoding

In order to avoid the problem of inconsistent character set encoding, we can set a unified character set encoding in the request header.

For example, we can set in the request header:

Content-Type: application/x-www-form-urlencoded;charset=utf-8

In this way, when receiving a request body containing Chinese characters in Node.js, it can be decoded uniformly by using UTF-8 encoding .

  1. Forcing the character set encoding to be specified

In addition to setting the character set encoding in the request header, we can also force the character set encoding to be specified for decoding.

For example, when using the express framework, we can use the body-parser middleware and specify the character set encoding for decoding. The example is as follows:

const bodyParser = require('body-parser')
const app = express()

app.use(bodyParser.urlencoded({ extended: false, limit: '50mb', parameterLimit: 10000, type: 'application/x-www-form-urlencoded', verify: (req, res, buf) => { req.raw = buf } }))
  1. Use the iconv-lite module to decode

Another method is to use the iconv-lite module to decode, the code example is as follows:

const iconv = require('iconv-lite')
const buffer = Buffer.from('e4b8ade69687', 'hex')
const str = iconv.decode(buffer, 'utf8')
console.log(str) // 输出:"中文"
  1. Use the text-encoding module for decoding

The last solution is to use the text-encoding module for decoding.

For example, when using the express framework, we can use the text-encoding decoder for our request body parser and set it to utf-8. An example is as follows:

const express = require('express')
const bodyParser = require('body-parser')
const { TextDecoder } = require('text-encoding')

const app = express()
app.use(bodyParser.json({ verify: function(req, res, buf) { req.body = JSON.parse(new TextDecoder('utf-8').decode(buf)) }}))

Summary

This article introduces the garbled problem that may occur when Node.js receives a request containing Chinese characters, and several methods to solve this problem.

In Node.js development, it is very important to correctly handle the problem of garbled Chinese characters. To solve this problem, we can use unified character set encoding, forcefully specify character set encoding, use the iconv-lite module to decode, and use the text-encoding module to decode, etc., so as to avoid garbled characters and ensure the normal operation of the application. I hope this article can help developers solve this problem.

The above is the detailed content of Garbled Chinese characters received by nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn