Home >Web Front-end >Front-end Q&A >What should I do if node returns garbled characters?

What should I do if node returns garbled characters?

藏色散人
藏色散人Original
2022-12-29 14:02:192792browse

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)
})

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!

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