Home >Web Front-end >Front-end Q&A >What should I do if node returns garbled characters?
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')".
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
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!