What should I do if node returns garbled characters?

藏色散人
Release: 2022-12-29 14:02:19
Original
2737 people have browsed it

Solution to the garbled code returned by node: 1. Open the corresponding node module file; 2. Manually set the encoding format of the content, and the modified code is "res.setHeader("Content-Type", 'text/html ; charset=utf-8')".

What should I do if node returns garbled characters?

The operating environment of this tutorial: Windows 10 system, node v7.6.0 version, Dell G3 computer.

What should I do if node returns garbled characters?

Solution to the problem of Chinese garbled content returned by the Nodejs http module

When the rs.end() method is called to send Chinese content to the client, the garbled problem will occur. At this time, You need to manually set the encoding format of the content:

Remember to re-run the code after modification

server.on('request', (req, res) => {
    const url = req.url
    const method = req.method
    const s = `请求的url是 ${url}, 请求方法是 ${method}`
    console.log(s)
    // 调用res.end()方法,向服务器响应一些内容
    res.setHeader("Content-Type", 'text/html; charset=utf-8')
    res.end(s)
})
Copy after login

You can see that the returned content has been modified successfully

What should I do if node returns garbled characters?

Recommended learning: "react video tutorial"

The above is the detailed content of What should I do if node returns garbled characters?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!